STATIC
static에 대해서 알아보자
Last updated
public class StaticCar {
static String _where="I am a Car from Germany!";
Country _country; // object of inner class country
StaticCar(){
_country=new Country(); // instantiate the inner class
}
static class Country { // static member inner class
String showCountry() {
return _where;
}
}
public static void main(String[] args) {
StaticCar myCar = new StaticCar() ; // instantiated object of class StaticCar
System.out.print("Access through an Country reference");
System.out.println(" created in an object of a StaticCar:");
System.out.println(myCar._country.showCountry());
// instantiated object of class StaticCar.Country
StaticCar.Country country = new StaticCar.Country();
System.out.println("Access through an Country reference that is local:");
System.out.println(country.showCountry());
}
}public static void main(String[] args) {
// instantiated object of class StaticCar.Country
StaticCar.Country country = new StaticCar.Country();
StaticCar.Country country2 = new StaticCar.Country();
System.out.println(country == country2);
}public class StaticFruit {
private static int seeds; // 초기값이 0으로 설정됨
public StaticFruit(int seeds) {
this.seeds = seeds;
}
}
public class StaticFruit {
private final int seeds; // 인스턴스 변수라 인스턴스 생성시 결정됨
public StaticFruit(int seeds) {
this.seeds = seeds;
}
}
public class StaticFruit {
private static final int seeds = 2; // 불변이며 static 변수라 초기값을 지정해 주어야 한다.
public StaticFruit() {}
}public class StaticUtil {
private static final int MAX_COUNT = 10;
public static int getApply(int count){
return MAX_COUNT * count;
}
}public class StaticTest {
private int count = 0;
private static String name = "";
static{
name = "incheol";
System.out.println("static block!!!!");
}
{
name = "test";
System.out.println(count);
System.out.println("non static block!!!");
}
public StaticTest(int count) {
this.count = count;
name = "test22";
System.out.println("construct block");
}
public static void main(String[] args) {
System.out.println(StaticTest.name);
}
}
// 결과값
// static block!!!!
// non static block!!!
// construct block
// test22