JAVA 13강 기타제어자

YEONG EUN LEE (Chloe)·2023년 12월 21일

JAVA 기초

목록 보기
18/42
post-thumbnail

기타제어자( Modifier )

  • 최종적인 final => ~ 할 수 없는, ~ 하지 못 하는
  • static => 정적인(고정적인), 공유되는, 객체 생성없이 클래스 이름으로 접근가능한
  • abstract => 추상적인, 몸체가 없는, 반드시 재정의 해야 하는

static 제어자

: 정적인(고정되어), 공유가 되는, 클래스 이름으로 접근 가능(=객체 생성 없이)

  • static(멤버로 위장) 키워드를 붙일 수 있는 것 : 생성자, 클래스에는 못 붙임, 멤버 필드(속성), 메소드, 초기화블럭에만 붙을 수 있음 예외는 존재함
  • static은 객체를 따로 생성하지 않고 생성할 수 있음. 누적으로 사용해야 할 경우 유용함. 프로그램 실행될 때부터 프로그램이 종료될 때 까지 실행되고 있음.
static 메소드static 없는 메소드 or 생성자
그냥 변수명으로 호출, class명.변수명으로 호출this로 호출할 수 있음
class Ex{
	int a; // 멤버
	static int b; // 멤버위장, static 변수, 정적 변수, 클래스 변수

	public void setA(int a){ // 멤버
		int c = c; // 지역변수
		this.a = a + c;
	}
	public static void setB(int b){ // 멤버 위장 // static메소드, 정적메소드
		this.b = b;
	}
	public static void md1( ){ // 멤버 위장
	System.out.println("md1호출");
	}
	public void md2( ){ // 멤버
	System.out.println("md2호출");}
}
class ExSub{
	int a; // 멤버
	static int b; // 멤버위장, static 변수, 정적 변수, 클래스 변수
	
	public ExSub() {
		this.b = 5; // (X)
	}

	public void setA(int a){ // 멤버
		int c = 5; // 지역변수
		this.a = a + c;
		this.b = 5;
	}
	public static void setB(){ // 멤버 위장 // static메소드, 정적메소드
		b = 5;
		a = 2; // static 메소드 안에는 static이 없는 멤버변수는 호출할 수 없음.
		this.a = 2; // 안됨 
	}
	public static void setB(int paramB){ // 멤버 위장 // static메소드, 정적메소드
		b = paramB;
	}
	public static void md1( ){ // 멤버 위장
	System.out.println("md1호출");
	}
	public void md2( ){ // 멤버
	System.out.println("md2호출");}
}

메모리 영역

Method = Static = Class = 정적
static
Ex
static int b;
public static void setB(int b){
this.b = b;
}
public static void md1( ){
System.out.println("md1호출");
}

class Ex{
	int a;
	static int b;


	public static void setB(  ){
		int c = b+5; // 지역변수
		// this.b = b; //(X) static 키워드가 붙어있는 변수에서는 this로 접근 할 수 없음
		Ex.b = b;
		this.b = c; // static 키워드가 붙어있는 변수에서는 this로 접근 할 수 없음
		System.out.println(b+c); //(O) static 메소드에서는 static 메소드 , static 멤버변수만 호출 가능함
		System.out.println(a+b+c);//(X)
		md1( );(X)
		md2( ); (O)//  
	}
	public voic setA(int a){
		this.a = a;
		System.out.println(a);
	}
	public void md1( ){ System.out.println(a);}
	public static void md2( ){System.out.println(b);}
}

0개의 댓글