
μλ° ν΄λμ€ μμμ λ³μλ λ©μλμ μ κ·Ό κΆνμ μ μ΄νλ λ¬Έλ²(μ κ·Ό μ μ΄μ, Access Modifier) λ₯Ό νμ΅ν©λλ€.
β μ½λ κ΄λ¦¬μ 보μμ μν΄ μ€μν κΈ°μ΄ κ°λ
μ
λλ€.
| ν€μλ | μλ―Έ | νΉμ§/μ¬μ©μ² |
|---|---|---|
public | μ΄λμλ μ κ·Ό κ°λ₯ | λ²μ© λ³μ, λ©μλ |
| (default) | μ무 κ²λ μμ°λ©΄ κ°μ ν¨ν€μ§ μμμλ§ μ κ·Ό κ°λ₯ (package-private) | ν¨ν€μ§ λ΄λΆ νμ μ© |
private | ν΄λμ€ λ΄λΆμμλ§ μ κ·Ό κ°λ₯ | μ 보 μλ, 보μ |
protected | κ°μ ν¨ν€μ§ + μμλ°μ ν΄λμ€μμ μ κ·Ό κ°λ₯ | μμ ꡬ쑰 νμ© μ |
static | κ°μ²΄ μμ± μμ΄ ν΄λμ€λͺ μΌλ‘ μ§μ μ κ·Ό κ°λ₯ | μ νΈμ± ν¨μ, μ μ μμ |
@Entity
public class Item {
public String title;
public Integer price;
}
// λ€λ₯Έ ν΄λμ€
Item item = new Item();
item.title = "λ°λ³΄"; // μ¬μ© κ°λ₯
@Entity
public class Item {
String title;
Integer price;
}
// κ°μ ν¨ν€μ§ λ΄λΆ
Item item = new Item();
item.title = "λ°λ³΄"; // κ°λ₯
@Entity
public class Item {
private String title;
private Integer price;
}
// λ€λ₯Έ ν΄λμ€
Item item = new Item();
item.title = "λ°λ³΄"; // β λΆκ°λ₯
@Entity
public class Item {
protected String title;
protected Integer price;
}
// κ°μ ν¨ν€μ§ μμμλ μ¬μ© κ°λ₯
// λ€λ₯Έ ν¨ν€μ§λ©΄ β λ¨, μμλ°μ ν΄λμ€λ μ κ·Ό κ°λ₯
@Entity
public class Item {
public static String title;
public static Integer price;
}
// κ°μ²΄ μμ± μν΄λ μ¬μ© κ°λ₯
Item.title = "μ μ λ³μ";
@Entity
public class Item {
private String title;
// getter
public String getTitle() {
return title;
}
// setter
public void setTitle(String title) {
this.title = title;
}
}
// μ¬μ©
Item item = new Item();
item.setTitle("μ
μΈ ");
System.out.println(item.getTitle());
Lombok μλ μμ±
@Entity
@Getter
@Setter
public class Item {
private String title;
private Integer price;
}
public: μλΉμ€ λ©μλ(API) κ³΅κ° private: λΉλ°λ²νΈ, λ΄λΆ μν κ° λ³΄νΈ protected: μμ ꡬ쑰μμ νμν λ°μ΄ν° μ λ¬ static: κ³΅ν΅ μ νΈ ν¨μ (μ: Math.random()) publicλ§ μ°λ©΄ 보μμ μ·¨μ½νλ€. private + getter/setter λ°©μμ΄ μμ νκ³ μ μ§λ³΄μμλ κ°νλ€. staticμ νΈλ¦¬νμ§λ§ λ¨μ©νλ©΄ κ°μ²΄μ§ν₯μ μ₯μ (볡μ¬/μ¬μ¬μ©μ±)μ μλλ€. private + getter/setter ν¨ν΄μ λ§μ΄ μ΄λ€. staticμ κ°μ²΄ μμ± μμ΄ ν΄λμ€λͺ
μΌλ‘ λ°λ‘ μ¬μ© κ°λ₯νλ€.