[JAVA] °­ÁÂ 4





|4ÀÏÂ÷|



Java¿¡¼­ÀÇ °´Ã¼ÁöÇâ ÇÁ·Î±×·¡¹Ö °­Á (3)


classÀÇ ¸â¹ö º¯¼ö¿Í ¸Þ¼Òµå... ±×¸®°í instanceÀÇ ¸â¹ö º¯¼ö¿Í ¸Þ¼Òµå....¿¡ ´ëÇÏ¿© ¾Ë¾Æº¸°Ú½À´Ï´Ù.



class A {

 static int i = 10;

 public static void main(String[] args) {

    System.out.println(i);

 }

}

½ÇÇà°á°ú´Â ´ÙÀ½°ú °°´Ù.

C:\Java ¿¬½À\OOP>java A

10



Áï static Å°¿öµå·Î Á¤ÀÇµÈ º¯¼ö´Â class AÀÇ instance¸¦ »ý¼ºÇÏÁö ¾Ê°í¼­µµ °ªÀ» ¾òÀ» ¼ö°¡ ÀÖ´Ù.

±×·¯¸é method¸¦ staticÀ¸·Î Á¤ÀÇÇغ¸ÀÚ.



class A {

 static void getInfo() {

    System.out.println("class AÀÇ static getInfo() ½ÇÇà");

 }

 public static void main(String[] argsÙ´?¸»ÀÌ ³ª¿Â´Ù.



C:\Java ¿¬½À\OOP>javac A.java

A.java:5: Can't make a static reference to nonstatic variable i in class A.

    System.out.println(i);

                       ^

1 error



Áï, static method¿¡¼­´Â static variable¸¸ ÂüÁ¶°¡ °¡´ÉÇÏ´Ù´Â °ÍÀÌ´Ù.

Äڵ带 Á¦´ë·Î °íÄ¡¸é ¾Æ·¡¿Í°°´Ù.



class A {

 static int i = 10;

 static void getInfo() {

    System.out.println(i);

 }

 public static void main(String[] args) {

    getInfo();

 }

}

½ÇÇàÇÏ¸é ´ÙÀ½°ú °°´Ù.

C:\Java ¿¬½À\OOP>java A

10



--------------------------------------------------------------------------------


static º¯¼öÀÇ overridingÀ» ¾Ë¾Æº¸ÀÚ

class A {

 static int i = 10;

}



class B extends A {

 static int i = 20;

 public static void main(String[] args) {

    System.out.println(i);

 }

}

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

Áï instance º¯¼öÀÇ overriding°ú ¸¶Âù°¡Áö·Î

static º¯¼öÀÇ overriding¶ÇÇÑ super classÀÇ staticº¯¼öÀÎ i=10À»

¼û±â°í sub classÀÎ class B¿¡¼­ »õ·ÎÀÌ Á¤ÀÇÇÑ i=20ÀÌ ÂüÁ¶µÇ¾î

È­¸é¿¡ Ãâ·ÂµÈ´Ù.



C:\Java ¿¬½À\OOP>java B

20



--------------------------------------------------------------------------------


±×·¯¸é overridingµÈ static º¯¼öÀÇ super class¿¡ Á¸ÀçÇÏ´Â static º¯¼ö¸¦ Ãâ·ÂÇÏ·Á¸é ¾î¶»°Ô ÇÏ¸é µÉ °ÍÀΰ¡ ?

class A {

 static int i = 10;

}



class B extends A {

 static int i = 20;

 public static void main(String[] args) {

    System.out.println(A.i);

 }

}

½ÇÇà°á°ú´Â ´ÙÀ½°ú °°´Ù. overridingµÈ static º¯¼öÀÇ super classÀÇ static º¯¼ö¸¦ »ç¿ëÇÏ°íÀÚ ÇÑ´Ù¸é static º¯¼ö°¡ ¼Ò¼ÓµÈ class ÀÇ À̸§À» »ç¿ëÇÏ¿© ±¸ÇϸéµÈ´Ù.

C:\Java ¿¬½À\OOP>java B

10



static º¯¼ö°¡ overridingµÇ¾ú´õ¶óµµ class AÀÇ static º¯¼ö i°¡ Ãâ·ÂµÇ¾ú´Ù.

½ÇÁ¦·Î sub class¿¡¼­ overridingµÈ static º¯¼ö¸¦ ÂüÁ¶ÇÏ´Â °ÍÀº sub classÀÇ À̸§ÀÌ »ý·«µÈ °Í°ú °°Àº È¿°ú¸¦ °¡Áø´Ù.

class A {

 static int i = 10;

}

class B extends A {

 static int i = 20;

 public static void main(String[] args) {

    System.out.println(i);

    System.out.println(B.i);

 }

}

½ÇÇà°á°ú¸¦ º¸¸é ¾Ë ¼ö ÀÖ´Ù. sub classÀÎ class B¿¡¼­ Ŭ·¡½º¸¦ ½áÁÖÁö ¾Ê°í ±×³É i¶ó°í ½áÁÖ¸é sub classÀÚü¿¡¼­ ã°ÔµÈ´Ù. ÀÌ·¯ÇÑ È¿°ú´Â sub classÀÎ class B¸¦ ¸í½ÃÀûÀ¸·Î Àû¾îÁÖ°í »ç¿ëÇÏ´Â °Í°ú °°Àº È¿°ú¸¦ °¡Áø´Ù.

C:\Java ¿¬½À\OOP>java B

20

20



--------------------------------------------------------------------------------


static º¯¼öÀ̸§Àº °°°í typeÀÌ ´Ù¸¥ °æ¿ì¸¦ »ìÆ캸ÀÚ. ÀÌ¿Í °°Àº °æ¿ì´Â ÄÄÆÄÀÏ ¿¡·¯¸¦ ¾ß±âÇÑ´Ù.

class A {

 static int i=10;

 static boolean i=true;

 public static void main(String[] arg) {

    System.out.println(i);

 }

}



C:\Java ¿¬½À\OOP>javac A.java

A.java:4: Duplicate variable declaration: boolean i was int i

 static boolean i=true;

                 ^

1 error



--------------------------------------------------------------------------------


static ¸â¹ö¿¡ ´ëÇÑ ¹Ì¹¦ÇÑ ³»¿ëÀ» ´ã°íÀÖ´Â ¿¹Á¦¸¦ ¸¸µé¾îº¸¾Ò´Ù.

class A {

 static int i=10;

 static boolean b=true;

}

class B extends A {

 static int i=20;

 public static void main(String[] arg) {

    System.out.println(i);

    System.out.println(B.i);

    System.out.println(A.i);

    System.out.println(b);

    System.out.println(B.b);

    System.out.println(A.b);

 }

}

½ÇÇà°á°ú´Â ´ÙÀ½°ú °°´Ù. ÀÌ·¯ÇÑ ¹®Á¦ÀÇ ´äÀ» ¸ÂÃá´Ù¸é »ó¼ÓÀ» ÀÌÇØÇÑ °ÍÀ̶ó »ý°¢µË´Ï´Ù.

C:\Java ¿¬½À\OOP>java B

20

20

10

true

true

true







´ÙÀ½¿¡´Â StringÀÇ trim() methodÀÌ ¿Ã¶ó¿Ã ¿¹Á¤ÀÔ´Ï´Ù.