영상
개념
package 스태틱1_개념;
class Product{
static public int count;
public String name;
public Product(){
Product.count += 1;
}
}
public class 스태틱1_개념04_기본이론4 {
public static void main(String[] args) {
for(int i = 0; i < 10; i++) {
Product p = new Product();
}
// 현재까지 생성된 Product갯수를 알수있다. (메모리 누수확인용)
System.out.println(Product.count);
}
}
Java
복사
package 스태틱1_개념;
class Monster {
static int count;
void init() {
System.out.println("몬스터가 탄생했습니다.");
count += 1;
}
void die() {
System.out.println("몬스터가 죽었습니다.");
count -= 1;
if(count == 0) {
System.out.println("몬스터가 전멸했습니다.");
} else {
System.out.println("현재 몬스터는 " + count + "명 남았습니다.");
}
}
}
public class Ex {
public static void main(String[] args) {
Monster m1 = new Monster();
m1.init();
Monster m2 = new Monster();
m2.init();
Monster m3 = new Monster();
m3.init();
m1.die();
}
}
JavaScript
복사