πStatic μ΄λ?

static ν€μλλ λͺ¨λ κ°μ²΄κ° ν¨κ» μ¬μ©νλ λ³μλ λ©μλλ₯Ό λ§λ€λ μ¬μ©λ©λλ€.κ°μ²΄(μΈμ€ν΄μ€)λ₯Ό λ§λ€μ§ μμλ ν΄λμ€ μ΄λ¦λ§μΌλ‘ λ°λ‘ μ¬μ©ν μ μμ΅λλ€.static λ³μμ λ©μλλ ν λ²λ§ μμ±λκ³ Method Area(λ©μλμμ) μ μ μ₯λ©λλ€.π‘static ν€μλλ₯Ό νμ©ν΄ λ΄ μλ€.
static ν€μλλ λ³μ, λ©μλμ λΆμΌ μ μμ΅λλ€.static ν€μλλ‘ μ μΈλ λ³μμ λ©μλλ MethodArea μ μ μ₯λ©λλ€.
class Person {
// β
static λ³μ
static int population = 0;
// β
static λ©μλ
static void printPopulation() {
System.out.println("νμ¬ μΈκ΅¬ μ: " + population);
}
}
System.out.println("static λ³μ: " + Person.population);
System.out.println("static λ©μλ: " + Person.printPopulation);
πμΈμ€ν΄μ€ λ©€λ²λ?
λ³μμ λ©μλ μ
λλ€.Heap μμμ μμΉν©λλ€.
πμΈμ€ν΄μ€ λ³μλ₯Ό μμλ΄ μλ€.
name λ³μλ κ° κ°μ²΄λ§λ€ λ³λλ‘ μ μ₯λ©λλ€.class Person {
String name; // β
μΈμ€ν΄μ€ λ³μ
}
public class Main {
public static void main(String[] args) {
Person p1 = new Person(); // p1 κ°μ²΄ μμ±
p1.name = "gygim"; // β
p1 κ°μ²΄μ λ°μ΄ν°μ μ κ·Ό
Person p2 = new Person(); // p2 κ°μ²΄ μμ±
p2.name = "Steve"; // β
p2 κ°μ²΄μ λ°μ΄ν°μ μ κ·Ό
}
}

πμΈμ€ν΄μ€ λ©μλλ₯Ό μμ λ΄ μλ€.
class Person {
String name;
void printName() { // β
μΈμ€ν΄μ€ λ©μλ
System.out.println("λμ μ΄λ¦μ " + this.name + "μ
λλ€.");
}
}
public class Main {
public static void main(String[] args) {
Person p1 = new Person();
p1.name = "gygim";
p1.printName(); // β
p1 κ°μ²΄μ λ©μλ μ€ν
Person p2 = new Person();
p2.name = "Steve";
p2.printName(); // β
p2 κ°μ²΄μ λ©μλ μ€ν
}
}

πν΄λμ€ λ©€λ²λ₯Ό μμλ΄ μλ€.

λ³μμ λ©μλλ₯Ό μλ―Έν©λλ€.static ν€μλλ₯Ό μ¬μ©ν΄μ μ μΈν©λλ€.Method Area μ μ μ¬λ©λλ€.πν΄λμ€ λ³μλ₯Ό μμλ΄ μλ€.
Heap μ΄ μλλΌ Method Area μ μ μ₯λ©λλ€.ν΄λμ€λͺ
.λ³μλͺ
μΌλ‘ μ κ·Όκ°λ₯ν©λλ€.class Person {
static int population = 0; // β
ν΄λμ€ λ³μ
}
public class Main {
public static void main(String[] args) {
// β
κ°μ²΄ μμ± μ μλ ν΄λμ€ λ 벨μμ μ§μ μ κ·Όκ°λ₯
System.out.println("νμ¬ μΈκ΅¬ μ: " + Person.population);
Person p1 = new Person();
Person p2 = new Person();
// β
λͺ¨λ κ°μ²΄κ° νλμ κ°μ 곡μ
System.out.println("νμ¬ μΈκ΅¬ μ: " + Person.population);
}
}

πν΄λμ€ λ©μλλ₯Ό μμλ΄ μλ€.
class Person {
static int population = 0;
public Person(String name) {
this.name = name;
population++; // μμ±μ νΈμΆμ populataion 1 μ¦κ°
}
static void printPopulation() {
System.out.println("νμ¬ μΈκ΅¬ μ: " + population); // β
ν΄λμ€ λ©μλ
}
}
public class Main {
public static void main(String[] args) {
// β
κ°μ²΄μμ± μ¬λΆμ μκ΄μμ΄ μ¬μ© κ°λ₯
Person.printPopulation(); // νμ¬ μΈκ΅¬ μ: 0
Person p1 = new Person("gygim"); // μμ±μλ§λ€ population 1 μ¦κ°
Person p2 = new Person("Steve"); // μμ±μλ§λ€ population 1 μ¦κ°
Person.printPopulation(); // νμ¬ μΈκ΅¬ μ: 2
}
}

π‘μ 체 μ½λμ κ·Έλ¦ΌμΌλ‘ μμλ΄ μλ€.
static μΌλ‘ μ μΈλ λ³μμ λ©μλλ κ°μ²΄μμ 곡μ©μΌλ‘ μ¬μ©κ°λ₯ν©λλ€.public class Person {
static int population = 0; // ν΄λμ€ λ³μ (λͺ¨λ κ°μ²΄κ° 곡μ )
String name; // μΈμ€ν΄μ€ λ³μ
public Person(String name) {
this.name = name;
population++; // μμ±μ νΈμΆμ populataion 1 μ¦κ°
}
public void printName() {
System.out.println("μ΄λ¦: " + name);
}
public static void printPopulation() { // ν΄λμ€ λ©μλ
System.out.println("νμ¬ μΈκ΅¬ μ: " + population);
}
}
public class Main {
public static void main(String[] args) {
Person p1 = new Person("gygim");
Person p2 = new Person("Steve");
p1.printName(); // μΈμ€ν΄μ€λ©μλλ κ°μ²΄ μΈμ€ν΄μ€ν μ΄νμ νΈμΆ κ°λ₯
p2.printName();
// ν΄λμ€ λ©μλλ ν΄λμ€ μ΄λ¦μΌλ‘ νΈμΆ κ°λ₯
Person.printPopulation(); // "νμ¬ μΈκ΅¬ μ: 2"
}
}

β οΈstatic μ 곡μ κ° νμν κ³³μ μ¬μ©ν΄μΌν©λλ€.
public class Student {
static String name; // β οΈ λͺ¨λ κ°μ²΄κ° λμΌν nameμ 곡μ (μν)
public Student(String name) {
this.name = name;
}
public void printName() {
System.out.println("μ΄λ¦: " + name);
}
}
public class Main {
public static void main(String[] args) {
Student s1 = new Student("gygim");
Student s2 = new Student("Steve");
s1.printName(); // β οΈ "μ΄λ¦: Steve"
s2.printName(); // β οΈ "μ΄λ¦: Steve"
}
}

β οΈStatic λ©μλμμλ μΈμ€ν΄μ€λ³μμ μ κ·Όν μ μμ΅λλ€.
public class Person {
String name;
public static void staticMethod() {
System.out.println(this.name); // β οΈ μ€λ₯ λ°μ
}
}
public class Example {
int instanceVar = 10;
public static void main(String[] args) {
Example ex = new Example();
System.out.println(ex.instanceVar); // β
μ μ μΆλ ₯
}
}
β οΈstatic λ³μμ λ©λͺ¨λ¦¬λ νλ‘κ·Έλ¨μ΄ μ’
λ£λ λκΉμ§ λ©λͺ¨λ¦¬μ μ μ§λ©λλ€.
static λ¨μ©νλ©΄ λ©λͺ¨λ¦¬ λλΉλ‘ μ΄μ΄μ§λλ€.