컴퓨터는 잘못이 없다..

[JAVA] static자원을 포함하고 있는 클래스(static필드, static메소드,static메소드에서는 static자원만 사용가능하다!) 본문

공부/JAVA

[JAVA] static자원을 포함하고 있는 클래스(static필드, static메소드,static메소드에서는 static자원만 사용가능하다!)

도토리까꿍v 2020. 11. 28. 17:14
Contents 접기

[예제코드]

 

MyUtil.java (MyUtil 클래스)

package test.mypac;
/*
static 자원을 포함하는 용도로 사용해보기
static 붙은 메소드나, 필드를 static 메소드/필드 라고 말하고
static이 붙지 않은 메소드나, 필드를 non-static 메소드/필드 라고 말한다.
*/
public class MyUtil {
	
	//non-static 핃드
	public String name;
	
	//static은 딱 한번만 static영역에 생긴다.
	//singleton의 의미를 가진다.
	public static String owner;
	
	//non-static 메소드
	public void call() {
		//non-static메소드에서는 static자원 non-static 자원 둘다 사용 가능하다.
		System.out.println(owner + "가" +name +" 을(를) 부릅니다.");
	}
	
	//static 메소드
	//static메소드 안에서는 static자원만 접근할 수 있다.
	//또한 this는 객체 생성과 관련이 있어 객체 생성이 되면 그 객체를 가리키는 역할을 하는데
	//static 변수의 경우 객체생성을 안하기 때문에 this를 사용할 수 없다. 
	public static void playMusic() {
		//static 메소드 안에서의 지역변수 사용가능 
		String a = "님이"; 
		
		//클래스의 필드를 쓰려면 static 자원만 사용가능, static메소드 안에서는 static자원만 접근 가능
		System.out.println(owner + a +"음악을 틀어요!"); 
		
		//owner은 static 자원이므로 클래스명.~ 으로도 사용가능하다. 
		System.out.println(MyUtil.owner + a + "음악을 틀어요!"); 
		
		//int c = this.owner; //--> 에러남!(static필드 사용가능하지만 this를 썼기때문에 에러남)
		//int a = this.version; //--> 에러남!(static자원엔 접근 불가하며, this를 썼기때문에 에러남)
		//int b = MyUtil.version; //--> 에러남!(static 자원이 아니기때문에 에러남) 
	}
}


 

MainClass02.java

package test.main;

/*
일반 필드와 메소드
static 필드와 메소드 차이점을 꼭 알아두기!
*/

import test.mypac.MyUtil;

public class MainClass03 {
	public static void main(String[] args) {
		//class 에 static이 붙지 않은 필드, 메소드는 new 로 참조값을 생성한 후 접근 가능!
		//class 에 static이 붙은 필드, 메소드는 클래스명.~으로의 접근해야한다!
	
		
		//staic자원 사용해보기1
		//MyUtil클래스에 정의된 static 메소드 호출하기
		MyUtil.playMusic(); //null가 음악을 틀어요!
		
		//static자원 사용해보기2
		//MyUtil 클래스에 정의된 static 필드 참조해서 값 대입하기.
		MyUtil.owner = "김구라";
		MyUtil.playMusic(); //김구라가 음악을 틀어요!
		
		//non-static자원 사용해보기
		MyUtil m = new MyUtil();
		m.name="해골";
		m.call();

	}
}

 

[MainCalss02 실행결과]

null님이음악을 틀어요!
null님이음악을 틀어요!
김구라님이음악을 틀어요!
김구라님이음악을 틀어요!
김구라가해골 을(를) 부릅니다.

 

 

[코드 설명]

 

1. static자원과 non-static자원이 생기는 영역

static자원은 객체생성을 하지 않는다!! static영역에 딱 한번 생성된다.

non-static자원은 객체생성을 할 때마다 heap영역에 생긴다. 

2. 아래 그림을 참고하여 퀴즈 풀어보기

Q1)static영역에 out이라는 필드에 41번지 참조값이 들어있다.

이때 heap영역의 print(){}로 실행순서를 옮기는 방법은?

A1)Other.out.print();

 

Q2)Car car1 = new Car();

Car car2 = new Car(); 했을 때 지역변수와 실제 객체는 어디에 생성될까?

A2) 지역변수는 stack에 생성되며 참조값을 저장하고 있다, 객체는 heap에 생긴다. 

 

Q3)static영역에 있는 owner과 playMusic()을 접근, 호출하는 방법?

MyUtil.owner;

MyUtil.playMusic();

 

3. static자원과 non-static자원을 

static 필드와 메소드는는 객체 생성을 하지 않고, 클래스명.~로 접근

non-static 필드와 메소드는 new 객체생성을 하고, 참조값.~로 접근

 

 

[static메소드에서는 static자원만 사용가능하다!!]

 

-static메소드에서는 static자원만 사용가능하다.

단, non-static메소드에서는 static자원, non-static자원 둘다 사용가능하다. 

 

-A클래스와 B클래스를 보며 static 자원 사용가능 여부 확인하기

1)static메소드에서는 static자원만 사용가능하다고 했는데,

A클래스는 static클래스가 아니고, a는 static 필드가 아니고, print()는 static메소드가 아닌데

왜? B클래스의 static메소드인 메인메소드에서 사용가능할까? 

 

2)B클래스의 Inner Class가 아닌 클래스들은 B클래스의 static메소드에서 static 자원이 아니어도 사용가능하다.

하지만 B클래스의 필드, 메소드, InnerClass는 B클래스의 static메소드에서 static자원이어야만 사용가능하다!

또한 InnerClass에 한해 static 예약어 붙이는 것을 허용한다!

 

 

 

-퀴즈 풀어보기

 

Q. (1)~(21)번 코드 가능하지 가능하지 않은 지 o,x로 풀어보기!

 

A.java 문제

package test.main;

public class A {
	
	public int a;
	public static int b;
	
	public void print() {
		System.out.println(a); //(1)
		System.out.println(b); //(2)
	}
	
	public static void print2() {
		System.out.println(a);//(3)
		System.out.println(b);//(4)
		
	}

}

 

B.java 문제 

package test.main;

public class B {

	class B1{
		public int b1=1;
	}
	
	static public class B2{
		public int b2=1;
	}
	
	public int c;
	public static int d;
	
	public void print3() {
		System.out.println(c); //(5)
		System.out.println(d); //(6)
	}
	
	public static void print4() {
		System.out.println(c); //(7)
		System.out.println(d); //(8)
	}
	
	public static void main(String[] args) {

		A ca=new A(); //(9)
		int num=ca.a; //(10)
		ca.print(); //(11)
		
		int num2=A.b; //(12)
		A.print2(); //(13)
		
		B1 cb1=new B1(); //(14)
		int num3=cb1.b1; //(15)
		
		B2 cb2=new B2(); //(16)
		int num4=cb2.b2; //(17)
		
		
		System.out.println(c);//(18)
		System.out.println(d);//(19)
		
		print3();//(20)
		print4();//(21)		
	}
}

 

A.java 답

package test.main;

public class A {
	
	public int a;
	public static int b;
	
	public void print() {
		System.out.println(a); //(1.o)
		System.out.println(b); //(2.o)
	}
	
	public static void print2() {
		System.out.println(a);//(2.x)
		System.out.println(b);//(4.o)
		
	}

}

 

 

B.java답

package test.main;

public class B {

	class B1{
		public int b1=1;
	}
	
	static public class B2{
		public int b2=1;
	}
	
	public int c;
	public static int d;
	
	public void print3() {
		System.out.println(c); //(5.o)
		System.out.println(d); //(6.o)
	}
	
	public static void print4() {
		System.out.println(c); //(7.x)
		System.out.println(d); //(8.o)
	}
	
	public static void main(String[] args) {

		A ca=new A(); //(9.o)
		int num=ca.a; //(10.o)
		ca.print(); //(11.o)
		
		int num2=A.b; //(12.o)
		A.print2(); //(13.o)
		
		B1 cb1=new B1(); //(14.x)
		int num3=cb1.b1; //(15.x)
		
		B2 cb2=new B2(); //(16.o)
		int num4=cb2.b2; //(17.o)
		
		
		System.out.println(c);//(18.x)
		System.out.println(d);//(18.o)
		
		print3();//(20.x)
		print4();//(21.o)	
		
	}
}
Comments