item 22 Jung inchul - JAVA-JIKIMI/EFFECTIVE-JAVA3 GitHub Wiki
Effective Java 3e μμ΄ν 22λ₯Ό μμ½ν λ΄μ© μ λλ€.
μΈν°νμ΄μ€λ μμ μ ꡬνν ν΄λμ€μ μΈμ€ν΄μ€λ₯Ό μ°Έμ‘°ν μ μλ νμ
μν
μ νλ€. κ·Έλ¦¬κ³ μΈν°νμ΄μ€
λ μ€μ§ μ΄ μ©λλ‘λ§ μ¬μ©ν΄μΌ νλ€.
μ΄ μ§μΉ¨μ λ§μ§ μλ μλ‘ μμ μμ μΈν°νμ΄μ€
λΌλ κ²μ΄ μλ€. μμ μΈν°νμ΄μ€λ λ©μλ μμ΄ μμλ₯Ό λ»νλ static final
νλλ‘λ§ κ°λ μ°¬ μΈν°νμ΄μ€λ₯Ό λ§νλ€.
public interface PhysicalConstants {
// μ보κ°λλ‘ μ (1/λͺ°)
static final double AVOGADROS_NUMBER = 6.022_140_857e23;
// λ³ΌμΈ λ§ μμ (J/K)
static final double BOLTZMANN_CONSTANT = 1.380_648)52e-23;
// μ μ μ§λ (kg)
static final double ELECTRON_MASS = 9.109_383_56e-31;
}
ν΄λμ€κ° μ΄λ€ μμ μΈν°νμ΄μ€λ₯Ό μ¬μ©νλ μ¬μ©μμκ²λ μλ¬΄λ° μλ―Έκ° μλ€.
μ€νλ € μ¬μ©μμκ² νΌλ
μ μ£ΌκΈ°λ νλ©°, λ μ¬νκ²λ ν΄λΌμ΄μΈνΈ μ½λκ° λ΄λΆ ꡬνμ ν΄λΉνλ μ΄ μμ λ€μ μ’
μ
λκ² νλ€.
finalμ΄ μλ ν΄λμ€κ° μμ μΈν°νμ΄μ€λ₯Ό ꡬννλ€λ©΄ λͺ¨λ νμ ν΄λμ€μ μ΄λ¦ 곡κ°μ΄ κ·Έ μΈν°νμ΄μ€κ° μ μν μμλ€λ‘ μ€μΌλμ΄ λ²λ¦°λ€.
μ€μΌλλ€λ μλ―Έλ μλμ κ°μ μλ―Έκ° μλκΉ?
public interface TestInterFace {
static String NAME = "incheol";
}
public class InterfaceImpl implements TestInterFace {
public static void main(String[] args) {
System.out.println(NAME);
}
}
// result : incheol
public class InterfaceImpl implements TestInterFace {
public static final String NAME = "test";
public static void main(String[] args) {
System.out.println(NAME);
}
}
// result : test
java.io.ObjectStreamContants
λ±, μλ° νλ«νΌ λΌμ΄λΈλ¬λ¦¬μλ μμ μΈν°νμ΄μ€κ° λͺ κ° μμΌλ, μΈν°νμ΄μ€λ₯Ό μλͺ» νμ©ν μμ΄λ λ°λΌ ν΄μλ μλλ€.
public interface ObjectStreamConstants {
/**
* Magic number that is written to the stream header.
*/
final static short STREAM_MAGIC = (short)0xaced;
/**
* Version number that is written to the stream header.
*/
final static short STREAM_VERSION = 5;
/* Each item in the stream is preceded by a tag
*/
/**
* First tag value.
*/
final static byte TC_BASE = 0x70;
/**
* Null object reference.
*/
final static byte TC_NULL = (byte)0x70;
/**
* Reference to an object already written into the stream.
*/
final static byte TC_REFERENCE = (byte)0x71;
/**
* new Class Descriptor.
*/
final static byte TC_CLASSDESC = (byte)0x72;
/**
* new Object.
*/
final static byte TC_OBJECT = (byte)0x73;
/**
* new String.
*/
final static byte TC_STRING = (byte)0x74;
/**
* new Array.
*/
final static byte TC_ARRAY = (byte)0x75;
...
}
μμλ₯Ό 곡κ°ν λͺ©μ μ΄λΌλ©΄ λ ν©λΉν μ νμ§κ° λͺ κ°μ§ μλ€. νΉμ ν΄λμ€λ μΈν°νμ΄μ€μ κ°νκ² μ°κ΄λ μμλΌλ©΄ κ·Έ ν΄λμ€λ μΈν°νμ΄μ€ μ체μ μΆκ°ν΄μΌ νλ€. λͺ¨λ μ«μ κΈ°λ³Έ νμ
μ λ°μ± ν΄λμ€κ° λνμ μΌλ‘, Integer
μ Double
μ μ μΈλ MIN_VALUE
μ MAX_VALUE
μμκ° μ΄λ° μλ€.
public final class Integer extends Number implements Comparable<Integer> {
/**
* A constant holding the minimum value an {@code int} can
* have, -2<sup>31</sup>.
*/
@Native public static final int MIN_VALUE = 0x80000000;
/**
* A constant holding the maximum value an {@code int} can
* have, 2<sup>31</sup>-1.
*/
@Native public static final int MAX_VALUE = 0x7fffffff;
...
}
μ ν©ν μμλΌλ©΄ μ΄κ±° νμ
μΌλ‘ λ§λ€μ΄ 곡κ°νλ©΄ λλ€. κ·Έκ²λ μλλΌλ©΄, μΈμ€ν΄μ€ν ν μ μλ μ νΈλ¦¬ν° ν΄λμ€
μ λ΄μ 곡κ°νμ.
pakcage ...constantutilityclass;
public class PhysicalConstants {
private PhysicalConstants() {} // μΈμ€ν΄μ€ν λ°©μ§
// μ보κ°λλ‘ μ (1/λͺ°)
static final double AVOGADROS_NUMBER = 6.022_140_857e23;
// λ³ΌμΈ λ§ μμ (J/K)
static final double BOLTZMANN_CONSTANT = 1.380_648)52e-23;
// μ μ μ§λ (kg)
static final double ELECTRON_MASS = 9.109_383_56e-31;
}
μ νΈλ¦¬ν° ν΄λμ€μ μμλ₯Ό λΉλ²ν μ¬μ©νλ€λ©΄ μ μ μν¬νΈ(static import)
νμ¬ ν΄λμ€ μ΄λ¦μ μλ΅ν μ μλ€.
import static ...PhysicalConstants.*;
public class Test {
double atoms(double mols) {
return AVOGADROS_NUMBER * mols;
}
...
}
μΈν°νμ΄μ€λ νμ
μ μ μνλ μ©λλ‘λ§ μ¬μ©ν΄μΌ νλ€. μμ 곡κ°μ© μλ¨μΌλ‘ μ¬μ©νμ§ λ§μ.