Computers/Language java

Access specifier / Modifier

emzei 2011. 4. 10. 20:02

 

 

Access specifier

 

접근자 관련 참고 링크(+출처):

:http://download.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

 

 

♩ Top Level - public, no modifier

 Member Level - public, private, protected, package-private

 

 public 

 - visible to all classes everywhere

 

 private 

 - member can only be accessed in its own class

 

 protected 

 - member can only be accessd within itw own package

+ by a subclass of its class in another package

 

 no modifier (default, package-private)

 - visible only within its own package (※packages are named groups of related classes)

 

 

 Access Level

ModifierClassPackageSubclassWorld
publicYYYY
protectedYYYN
no modifierYYNN
privateYNNN

 

 

 

 

 

 

 

Modifier

 

참고하면 좋은 링크(+출처) :

http://download.oracle.com/javase/tutorial/java/javaOO/classvars.html

 

 static 

- static modifier가 사용된 필드들은 static fields 또는 class variables라 한다.

- Every instance of the class shares a class variable, which is in one fixed location in memory.

- Any object can change the value of a class variable, but class variables can also be manipulated without creating an instance of the class.

- Static methods, which have the static modifier in their declarations, should be invoked with the class name, without the need for creating an instance of the class. (Static 함수는 Static 변수들만 참조가능)

- A common use for static methods is to access static fields.

 

=> point : share !

 

 

◆ final 

- The final modifier indicates that the value of this field cannot change.

 

=> point : can not change !

 




constructor는 오직 public, private, protected, default만 가능 ( modifier 불가 )


private로 constructor를 선언할 수 있지만, 해당 class 이외에서는 쓸 수 없음! ( private이니까 :> )


static 함수 내에서는 static 인자들만 사용가능하다


static은 공유하므로 유일무이하다는 것을 잊지 말자



static 변수는 상속받으면 하위클래스에서 상위클래스의 변수를 수정하면 수정된다

protected 변수는 상속받으면 하위클래스에서 상위클래스의 변수를 수정할 수 있다 


차이점

class T1{


//static이 있고 없고의 차이만 확실하게 짚고 가시길 


static int a = 5;

protected int b = 5;



}

 

 

class T2 extends T1{


public T2()

{

super();

system.out.println(super.a); 

super.a -= 1 ; 

system.out.println(super.a); 

}


public T2(int a)

{

super();

system.out.println(super.b);

super.b -= 1;

system.out.println(super.b);

}



public static void main(String [] ar)

{

system.out.println("\n[1]");

T2 a = new T2();

T2 b = new T2();


system.out.println("\n[2]");

T2 c = new T2(1);

T2 d = new T2(2);

}


}




<출력 결과>


[1]

5

4

4

3


[2]

5

4

5

4


// 변수가 공유되고 공유 되지 않는 다는 것!!! :>