1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package com.test;
 
public class static {
 
    //private int num; //x
    //private static int num2; //o
    
    public static void main(String[] args) {
        
        //num = 100; //x
        //num2 = 100; //o
        
        //Ex36_static.java
        
        
        //객체 멤버 접근 -> 객체 변수를 통해서.,
        StaticTest t1 = new StaticTest();
        t1.a =10;
        //t1.b =20;
                
        //정적 멤버 접근 -> 클래스명을 통해서..
        StaticTest.b = 20;
        
        m1();
        //m2(); //static을 없애고 굳이 호출해야겠다면 이렇게.,
        Ex36_static ex = new Ex36_static();
        ex.m2();
        
    }
 
    private void m2() {
        // TODO Auto-generated method stub
        
    }
 
    private static void m1() {
        // TODO Auto-generated method stub
        
    }
}
 
 
class StaticTest {
    
    public int a = 10;            //객체변수
    public static int b = 20//정적변수
    
    //객체 메소드 -> 멤버 변수 접근(객체 변수 접근)
    public void m1() {
        System.out.printf("a = %d\n", a);
        System.out.printf("a = %d\n", b);
    }
    //정적 메소드 -> 멤버 변수 접근(정적 변수 접근)
    public static void m2() {
        //정적 영역에서는 this 키워드를 인식 못하기 때문..****
        //System.out.printf("a = %d\n", a); ->a에 빨간줄 뜸
        System.out.printf("a = %d\n", b);
    }
}
cs


'WEB > JAVA' 카테고리의 다른 글

CONSTRUCTOR(생성자)2  (0) 2015.05.22
CONSTRUCTOR(생성자)1  (0) 2015.05.22
STATIC(정적)1  (0) 2015.05.22
ACCESS(접근지정자)  (0) 2015.05.22
CLASS  (0) 2015.05.22

+ Recent posts