[JAVA] °­ÁÂ 14



|14ÀÏÂ÷|



Java º¯¼öÀÇ ÃʱâÈ­
¸í½ÃÀûÀ¸·Î °ªÀ» ÇÒ´çÇØ ÁÖÁö ¾ÊÀ» °æ¿ì JavaÀÇ ÃʱâÈ­¸¦ ¾Ë¾Æº»´Ù

°ªÀ» ÇÒ´çÇϱâ Àü¿¡ º¯¼ö¿¡ ¾î¶² °ªÀÌ ÀÚ¹Ù¿¡¼­ ÃʱâÈ­½ÃÅ°´ÂÁö ¾Ë¾Æº¸ÀÚ.

class A {
 static int i;
 static boolean b;
 int ii;
 boolean bb;
 public static void main(String[] args) {
    System.out.println(i);
    System.out.println(b);
    A a = new A();
    System.out.println(ii);
    System.out.println(bb);
 }
}
¿¡·¯°¡ ÀÖ´Â ½ÇÇà°á°ú´Â ´ÙÀ½°ú °°½À´Ï´Ù.

C:\Java ¿¬½À\OOP>javac A.java
A.java:11: Can't make a static reference to nonstatic variable ii in class A.
    System.out.println(ii);
                       ^
1 error

static method ¿¡¼­´Â static method°¡ ¼ÓÇÑ Å¬·¡½ºÀÇ static º¯¼ö¸¸À» Á¢±ÙÇÒ ¼ö ÀÖ½À´Ï´Ù.

¿Ã¹Ù¸¥ ÄÚµå´Â ¾Æ·¡¿¡ ÀÖ½À´Ï´Ù.

class A {
 static int i;
 static boolean b;
 int ii;
 boolean bb;
 public static void main(String[] args) {
    System.out.println(i);
    System.out.println(b);
    A a = new A();
    System.out.println(a.ii);
    System.out.println(a.bb);
 }
}
½ÇÇà°á°ú´Â ¾Æ·¡¿Í °°´Ù.

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

Áï, ÇÁ·Î±×·¡¸Ó°¡ Á÷Á¢ ÃʱâÈ­¸¦ ÇØÁÖÁö ¾Ê´õ¶óµµ ±âº»ÀûÀÎ °ªÀ¸·Î ÃʱâÈ­°¡ ÀÌ·ç¾îÁö±â ¶§¹®¿¡ ¾²·¹±â °ªÀÌ µé¾î°¡Áö ¾Ê´Â´Ù.

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

instance°¡ ½ÇÁ¦·Î´Â ¸¸µé¾î Áö±âÀü¿¡ instance ÂüÁ¶º¯¼ö ¿¡´Â ¾î¶² °ªÀÌ µé¾î°¡ ÀÖ´ÂÁö ¾Ë¾Æº¸°Ú½À´Ï´Ù.

class A {
 int i=10;
 public static void main(String[] args) {
    A a ;
    System.out.println(a);
 }
}
compile ½Ã¿¡ error°¡ ³³´Ï´Ù.

C:\Java ¿¬½À\OOP>javac A.java
A.java:6: Variable a may not have been initialized.
    System.out.println(a);
                       ^
1 error

int, boolean °ú °°Àº Á¾·ùÀÇ º¯¼ö typeÀº ÃʱâÈ­¸¦ °ÅÄ¡Áö ¾Ê´õ¶óµµ ±âº»ÀûÀ¸·Î µðÆúÆ®°ªÀÌ µé¾î°©´Ï´Ù. ±×·¯³ª instance¸¦ °¡¸®Å°°íÀÖ´Â instanceÂüÁ¶º¯¼ö´Â ÃʱâÈ­¸¦ °ÅÃľ߸¸ »ç¿ëÇÒ ¼ö°¡ ÀÖ½À´Ï´Ù. ´ÙÀ½ Äڵ带 º¸µµ·Ï ÇÏ°Ú½À´Ï´Ù.

class A {
 int i=10;
 public static void main(String[] args) {
    A a ;
    a = new A();
    System.out.println(a);
    System.out.println(a.i);
 }
}
½ÇÇà°á°ú´Â ¾Æ·¡¿Í °°½À´Ï´Ù.

C:\Java ¿¬½À\OOP>java A
A@1cc765
10

instanceÂüÁ¶º¯¼öÀÎ aÀÚü¸¦ È­¸é¿¡ ÇÁ¸°Æ®Ç϶ó°í Çϸé ascii Äڵ尡 ³ª¿À´Âµ¥ ÀÌ°ÍÀº instance aÀÇ ¸Þ¸ð¸®»óÀÇ ÁÖ¼Ò¶ó°í »ý°¢µË´Ï´Ù. ±×·¯³ª a.i ¶ó°í Çϸé instanceÂüÁ¶º¯¼öa°¡ °¡¸®Å°´Â ÁÖ¼Ò Áï ½ÇÁ¦·Î ¸Þ¸ð¸®»ó¿¡ ¸¸µé¾îÁø instanceÀÇ iº¯¼öÀÇ °ªÀ» °¡Áö°í ¿À¶ó´Â °ÍÀÓÀ» ¾Ë ¼ö ÀÖ½À´Ï´Ù.

´ÙÀ½ Äڵ带 º¸µµ·Ï ÇÏ°Ú½À´Ï´Ù.

class A {
 public static void main(String[] args) {
    A a ;
    a = new A();
    if(a == null) System.out.println("instance a´Â nullÀÔ´Ï´Ù.");
    else System.out.println("instance a´Â nullÀÌ ¾Æ´Õ´Ï´Ù.");
 }
}
½ÇÇà°á°ú´Â ¾Æ·¡¿Í °°´Ù.

:\Java ¿¬½À\OOP>java A
nstance a´Â nullÀÌ ¾Æ´Õ´Ï´Ù.

instance°¡ ¸¸µé¾îÁ³À» ¶§ ³»¿ëÀÌ ¾Æ¹«°Íµµ ¾ø´Ù°í ÇÏ¿©µµ nullÀÌ ¾Æ´ÔÀ» ¾Ë ¼öÀÖ´Ù.
static method¶ÇÇÑ Á¸ÀçÇÏÁö ¾Ê´Â class ÀÇ instance¸¦ ¸¸µç ÈÄ ´Ù½Ã Å×½ºÆ® Çغ¸ÀÚ.

class A {
}
class B {
 public static void main(String[] args) {
    A a ;
    a = new A();
    if(a == null) System.out.println("instance a´Â nullÀÔ´Ï´Ù.");
    else System.out.println("instance a´Â nullÀÌ ¾Æ´Õ´Ï´Ù.");
 }
}
½ÇÇà°á°ú´Â ¾Æ·¡¿Í °°´Ù.

C:\Java ¿¬½À\OOP>java B
instance a´Â nullÀÌ ¾Æ´Õ´Ï´Ù.

static method Á¶Â÷ Á¸ÀçÇÏÁö ¾Ê´Â classÀÇ instanceÀÌ´õ¶óµµ null Àº ¾Æ´ÔÀ» ¾Ë ¼ö ÀÖ´Ù.





´ÙÀ½¿¡´Â Instance º¯¼ö¿Í »ó¼Ó¿¡ ´ëÇÑ ³»¿ëÀÌ ¿Ã¶ó¿Ã ¿¹Á¤ÀÔ´Ï´Ù.