[JAVA] °­ÁÂ 13



|13ÀÏÂ÷|



»ó¼Ó¿¡¼­ÀÇ class »ý¼ºÀÚ
»ó¼ÓµÈ classÀÇ static »ý¼ºÀÚ ½ÇÇà¿¡ ´ëÇÏ¿© ¾Ë¾Æº¸°Ú½À´Ï´Ù.

class A {
 static {
    System.out.println("class AÀÇ static »ý¼ºÀÚ ³»ºÎÀÔ´Ï´Ù.");
 }
}
class B extends A {
 static {
    System.out.println("class BÀÇ static »ý¼ºÀÚ ³»ºÎÀÔ´Ï´Ù.");
 }
}
class C extends B {
 static {
    System.out.println("class CÀÇ static »ý¼ºÀÚ ³»ºÎÀÔ´Ï´Ù.");
 }
 public static void main(String[] args) {
 }
}


½ÇÇà°á°ú

C:\Java ¿¬½À\OOP>java C
class AÀÇ static »ý¼ºÀÚ ³»ºÎÀÔ´Ï´Ù.
class BÀÇ static »ý¼ºÀÚ ³»ºÎÀÔ´Ï´Ù.
class CÀÇ static »ý¼ºÀÚ ³»ºÎÀÔ´Ï´Ù.

¶ì¿ë~
Java Virtual MachineÀÌ public static void mainÀ» È£ÃâÇÒ ¶§ class CÀÇ static »ý¼ºÀÚ°¡ È£ÃâÀÌ µË´Ï´Ù. class C´Â class B¿¡¼­ »ó¼ÓµÇ¾ú°í class B´Â class A·Î ºÎÅÍ »ó¼ÓÀ̵Ǿú½À´Ï´Ù.
ã¾Æ°¡´Â ¼ø¼­´Â class C , class B , class AÀÌÁö¸¸ ½ÇÁ¦·Î ½ÇÇàÀÌ µÇ´Â ¼ø¼­´Â class A, class B, class CÀÇ ¼ø¼­·Î ½ÇÁ¦·Î static »ý¼ºÀÚ°¡ È£ÃâÀÌ µË´Ï´Ù.

=================================================================

static »ý¼ºÀÚ¿Í public static void mainÀÇ ½ÇÇà ¿ì¼±¼øÀ§¸¦ ¾Ë¾Æº¾½Ã´Ù.

class A {
 static {
    System.out.println("class AÀÇ static »ý¼ºÀÚ ³»ºÎÀÔ´Ï´Ù.");
 }
}
class B extends A {
 static {
    System.out.println("class BÀÇ static »ý¼ºÀÚ ³»ºÎÀÔ´Ï´Ù.");
 }
}
class C extends B {
 static {
    System.out.println("class CÀÇ static »ý¼ºÀÚ ³»ºÎÀÔ´Ï´Ù.");
´Ù¸é static »ý¼ºÀÚ´Â »ó¼ÓµÇ´ÂÁö ¾Ë¾Æº¾½Ã´lass AÀÇ static »ý¼ºÀÚ ³»ºÎÀÔ´Ï´Ù.");
 }
 A () {
    System.out.printAÀÇ public static void mainÀÇ Ã¹¹®ÀåÀÔ´Ï´Ù.");
    A a;
 }
}


°á°ú´Â ¾Æ·¡¿¡ ÀÖ½À´Ï´Ù.

C:\Java ¿¬½À\OOP>java A
class AÀÇ static »ý¼ºÀÚ ³»ºÎÀÔ´Ï´Ù.
class AÀÇ public static void mainÀÇ Ã¹¹®ÀåÀÔ´Ï´Ù.

public static void main()ÀÌ ºÒ¸®¿öÁú ¶§ static »ý¼ºÀÚ´Â ½ÇÇàÀÌ ¹ú½á µÇ±â ¶§¹®¿¡ class AÀÇ instanceÂüÁ¶º¯¼ö a¸¦ ÁöÁ¤ÇÏ´õ¶óµµ »õ·ÎÀÌ class AÀÇ static »ý¼ºÀÚ°¡ ½ÇÇàµÇÁö´Â ¾Ê´Â´Ù´Â °ÍÀ» ¾Ë ¼ö ÀÖ½À´Ï´Ù.

´ÙÀ½À¸·Î ½ÇÁ¦·Î ¸Þ¸ð¸®°ø°£¿¡ class AÀÇ instance¸¦ »ý¼º½ÃÄѺ¸°Ú½À´Ï´Ù.

class A {
 static {
    System.out.println("class AÀÇ static »ý¼ºÀÚ ³»ºÎÀÔ´Ï´Ù.");
 }
 A () {
    System.out.println("class AÀÇ instance »ý¼ºÀÚ ³»ºÎÀÔ´Ï´Ù.");
 }
 public static void main(String[] args) {
    System.out.println("class AÀÇ public static void mainÀÇ Ã¹¹®ÀåÀÔ´Ï´Ù.");
    A a;
    a = new A();
 }
}


½ÇÇà°á°ú´Â ¾Æ·¡¿Í °°½À´Ï´Ù.

C:\Java ¿¬½À\OOP>java A
class AÀÇ static »ý¼ºÀÚ ³»ºÎÀÔ´Ï´Ù.
class AÀÇ public static void mainÀÇ Ã¹¹®ÀåÀÔ´Ï´Ù.
class AÀÇ instance »ý¼ºÀÚ ³»ºÎÀÔ´Ï´Ù.

class AÀÇ instance »ý¼ºÀÚ´Â ½ÇÁ¦·Î ¸Þ¸ð¸®°ø°£¿¡ instance¸¦ ¸¸µå´Â Å°¿öµåÀÎ 'new' °¡ ½ÇÇàÀÌµÉ ¶§ instance »ý¼ºÀÚ°¡ ½ÇÇàµÊÀ» ¾Ë ¼ö°¡ ÀÖ½À´Ï´Ù.

==========================================================================

instance »ý¼ºÀÚÀÇ »ó¼Ó¿¡ ´ëÇÏ¿© ¾Ë¾Æº¸°Ú½À´Ï´Ù.

class A {
 static {
    System.out.println("class AÀÇ static »ý¼ºÀÚ ³»ºÎÀÔ´Ï´Ù.");
 }
 A () {
    System.out.println("class AÀÇ instance »ý¼ºÀÚ ³»ºÎÀÔ´Ï´Ù.");
 }
}
class B extends A {
 static {
    System.out.println("class BÀÇ static »ý¼ºÀÚ ³»ºÎÀÔ´Ï´Ù.");
 }
}
class C extends B {
 static {
    System.out.println("class CÀÇ static »ý¼ºÀÚ ³»ºÎÀÔ´Ï´Ù.");
 }
 public static void main(String[] args) {
    System.out.println("mainÀÇ ³»ºÎÀÔ´Ï´Ù.");
    C c;
    c = new C();
 }
}
½ÇÇà°á°ú´Â ¾Æ·¡¿Í °°½À´Ï´Ù.

C:\Java ¿¬½À\OOP>java C
class AÀÇ static »ý¼ºÀÚ ³»ºÎÀÔ´Ï´Ù.
class BÀÇ static »ý¼ºÀÚ ³»ºÎÀÔ´Ï´Ù.
class CÀÇ static »ý¼ºÀÚ ³»ºÎÀÔ´Ï´Ù.
mainÀÇ ³»ºÎÀÔ´Ï´Ù.
class AÀÇ instance »ý¼ºÀÚ ³»ºÎÀÔ´Ï´Ù.

class B ¿Í class CÀÇ instance »ý¼ºÀÚ´Â Á¸ÀçÇÏÁö ¾Ê´Â´Ù. ±×·¯³ª class A¿¡´Â instance »ý¼ºÀÚ°¡ ÀÖ¾ú´Ù. class CÀÇ instance c¸¦ ¸¸µé ¶§ instance »ý¼ºÀÚ´Â top class±îÁö
ã¾Æ°¡¼­ ½ÇÇàÀÌ µË´Ï´Ù.

=============================================================

±×·¯¸é ´ÙÀ½À¸·Î instance »ý¼ºÀÚÀÇ ½ÇÇà ¼ø¼­¸¦ º¸°Ú½À´Ï´Ù.

class A {
 static {
    System.out.println("class AÀÇ static »ý¼ºÀÚ ³»ºÎÀÔ´Ï´Ù.");
 }
 A () {
    System.out.println("class AÀÇ instance »ý¼ºÀÚ ³»lass AÀÇ instance »ý¼ºÀÚ ³»ºÎÀÔ´Ï´Ù.
class BÀÇ instance »ý¼ºÀÚ ³»ºÎÀÔ´Ï´Ù.
class CÀÇ instance »ý¼ºÀÚ ³»ºÎÀÔ´Ï´Ù.

static »ý¼ºÀÚ³ª instance »ý¼ºÀÚ³ª ¶È°°ÀÌ top class·Î ºÎÅÍ ½ÇÇàÀÌ µÊÀ» ¾Ë ¼ö ÀÖ½À´Ï´Ù.





´ÙÀ½¿¡´Â JAVAº¯¼öÀÇ ÃʱâÈ­¿¡ ´ëÇÑ ³»¿ëÀÌ ¿Ã¶ó¿Ã ¿¹Á¤ÀÔ´Ï´Ù.