StaticEx01-static변수

CODEDRAGON Development/Java

반응형

   

static변수

인스턴스 이름을 이용한 접근방법

   

   

FileInfo

StaticEx01.java

main부

Static 변수 호출

StaticCount.java

static 변수 선언

   

   

   

출력결과

   

   

   

소스코드

StaticEx01.java

ublic class StaticEx01 {

        public static void main(String[] args) {
                
                StaticCount sc1= new StaticCount();
                System.out.println("c = " + sc1.c + "\t count = " + StaticCount.count);
                System.out.println("sc1 = " + sc1.count);       //
인스턴스 이름을 이용한 접근방법
//              System.out.println("sc1 = " + StaticCount.count); //
클래스 이름을 이용한 접근방법(권장)
                System.out.println();
                
                System.out.println("=================================");
                StaticCount sc2= new StaticCount();
                System.out.println("c = " + sc2.c + "\t count = " + StaticCount.count);
                System.out.println("sc2 = " + sc2.count);       //
인스턴스 이름을 이용한 접근방법
                System.out.println();
                
                System.out.println("=================================");
                StaticCount sc3= new StaticCount();
                System.out.println("c = " + sc3.c + "\t count = " + StaticCount.count);
                System.out.println("sc3 = " + sc3.count);       //
인스턴스 이름을 이용한 접근방법

        }
}

StaticCount.java

public class StaticCount {
        
        //
객체 생성시 heap영역에 객체가 만들어지고 객체에 포함
        int c;                          //
인스턴스 변수
        
        //
객체 생성과 관계 없음
        //
호출하게되면 static영역(메소드 영역) 만들어지며 공유개념을 가지게 됩니다.
        static int count;       //static
변수
        
        public StaticCount(){
                c++;
                count++;
        }
}

 


반응형