java에 관해 궁금한 두세가지 것들.

접근제한자 (Access Modifier)

static

정적변수(static variable) 혹은 클래스(class)변수라고 불림. 그 클래스로 부터 만들어진 모든 객체가 공유할 수 있는 변수를 말함. 즉, static으로 선언되어진 변수는 해당 클래스의 instance가 아무리 많이 생성되어져도 단 하나만 이용되어 진다.

* private, public 으로  선언되어 질 수 있다.

* public 으로 선언되어진 경우 class 밖에서 class의 이름만을 참조해서 사용되어 질 수 있다.

* Class 가 instance화, 즉 Object로 만들어지지 않고도 곧장 사용할 수 있으며, className.staic변수명 으로 사용될 수도 있기에 class변수라고도 불리움.

예문

class father{
 public int total = 0;
 public static int age = 0;

 public void display(){
  total+=1;
  age+=1;

  System.out.println("total = "+ total +" , " + "age = "+ age);
 }

}
public class Static_Example {
 public static void main(String[] args){
  father f1 = new father();
  f1.display();
  father f2 = new father();
  f2.display();
  father f3 = new father();
  f3.display();

 }
}

결과

total = 1 , age = 1
total = 1 , age = 2
total = 1 , age = 3

예문2.

public class Count {

    int var1; // instance(or member) 변수

    static int var2;

    public static void main(String [] args) {
     
            var1 = 7; // 띠옹~ instance 변수는 instance가 만들어지기 전까지 절대 사용못함.

             var2 = 8;

       }

}

결과

var1 부분 오류. 하지만 var2는 곧장 사용가능.

이 글과 관련있는 글을 자동검색한 결과입니다 [?]

by beizix | 2007/01/05 11:47 | JAVA | 트랙백 | 덧글(2)

트랙백 주소 : http://beizix.egloos.com/tb/771215
☞ 내 이글루에 이 글과 관련된 글 쓰기 (트랙백 보내기) [도움말]
Commented at 2007/01/05 21:27
비공개 덧글입니다.
Commented by beizix at 2007/01/11 22:36
ㅋㅋㅋㅋㅋ ㅇ ㅑ !!!!! 너 머야 !!!!

:         :

:

비공개 덧글

◀ 이전 페이지다음 페이지 ▶