4. μΈμ€ν΄μ€νλ₯Ό λ§μΌλ €λ©΄ private μμ±μ μ¬μ©
5. μμμ μ§μ λͺ μνμ§ λ§κ³ μμ‘΄ κ°μ²΄ μ£Όμ μ¬μ©
μ¬μ©νλ μμμ λ°λΌ λμμ΄ λ¬λΌμ§λ ν΄λμ€μλ μ μ μ νΈλ¦¬ν° ν΄λμ€λ μ±κΈν΄ λ°©μ μ ν© X
π μΈμ€ν΄μ€λ₯Ό μμ±ν λ μμ±μμ νμν μμμ λ겨주λ λ°©μ = μμ‘΄ κ°μ²΄ μ£Όμ
- μμ±μμ μμ ν©ν 리λ₯Ό λ겨μ€
β¨ ν©ν 리
π νΈμΆν λλ§λ€ νΉμ νμ μ μΈμ€ν΄μ€λ₯Ό λ°λ³΅ν΄μ λ§λ€μ΄μ£Όλ κ°μ²΄
public class SpellChecker{
private final Lexicon dictionary;
public SpellChecker(Lexicon dictionary){
this.dictionary = Objects.requireNonNull(dictionary);
}
public boolean isValid(String word){ ... }
public List<String> suggestions(String typo) { ... }
}
6. λΆνμν κ°μ²΄ μμ± νΌνκΈ°
7. λ€ μ΄ κ°μ²΄ μ°Έμ‘° ν΄μ νκΈ°
8. finalizerμ cleaner μ¬μ© μμ
9. try-finally보λ€λ try-with-resources μ¬μ©
μμμ΄ λ μ΄μμ΄λΌλ©΄ try-finally λ°©μμ μ§μ λΆν¨
try-with-resources(μλ° 7)μ μ¬μ©ν΄ 볡μμ μμμ μ²λ¦¬νλ λ° κ°λ μ±μ λμΌ μ μμ
public class TryWithResourcesExample {
public static void main(String[] args) {
// νμΌ κ²½λ‘λ₯Ό μ§μ
String filePath = "example.txt";
// try-with-resources λΈλ‘μ μ¬μ©νμ¬ μμμ μλμΌλ‘ λ«μ
try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
π ν΄λΉ μμμ΄ AutoCloseable μΈν°νμ΄μ€λ₯Ό ꡬνν΄μΌν¨(μλ° λΌμ΄λΈλ¬λ¦¬μ μλνν° λΌμ΄λΈλ¬λ¦¬λ€μ μλ§μ ν΄λμ€μ μΈν°νμ΄μ€κ° μ΄λ―Έ ꡬννκ±°λ νμ₯ν΄λ )
'Java > π Effective Java' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[π Effective Java] ν΄λμ€μ μΈν°νμ΄μ€ δΈ (2) | 2024.10.05 |
---|---|
[π Effective Java] ν΄λμ€μ μΈν°νμ΄μ€ δΈ (1) | 2024.09.25 |
[π Effective Java] λͺ¨λ κ°μ²΄μ κ³΅ν΅ λ©μλ δΈ (0) | 2024.09.22 |
[Effective Java] λͺ¨λ κ°μ²΄μ κ³΅ν΅ λ©μλ δΈ (0) | 2024.09.21 |
[π Effective Java] κ°μ²΄ μμ±κ³Ό νκ΄΄ δΈ (1) | 2024.09.16 |