프로그램 실행 시 해당 부분만 static 영역에 미리 생성
사용 할 때 : 여래개의 클래스에서 같은 데이터나 기능을 공유해서 사용 할 때
※ static은 공유자원이라고 생각하자.
A a = new A( ) ;
a.a( ) < = 객체 생성 영역 접근
A.a( ) < = static 에서 확인, static 영역 Ex) System.out
public static String s = "static 변수이지롱";
※ 두가지 경우
public static final String SF= "상수임"; //파이널 덕분에 값변경 안된다.
TestService6.SF = "Hi~"; //파이널때문에 값변경이 안된다.
클래스에서 static
public class SingletonService {
// 정적 변수 선언
private static SingletonService instance = null;
//객체취득 메소드
public static SingletonService getInstance() {
//큰 A getInstance에 주소값 abc
if(instance == null) {
//객체생성
instance = new SingletonService();
}
return instance;
}
public int a = 10;
}
public class TestService6 {
public void test() {
SingletonService ss = SingletonService.getInstance();
//인스턴스는 주소 만 담는다.
System.out.println(ss.a);
}
TestController6 tc = new TestController6();
SingletonService ss = SingletonService.getInstance();
System.out.println(ss.a);
ss.a = 7;
TestService6 ts = new TestService6();
ts.test();
//출력은 10 나온 후 7이 나온다
// syso
System.out.println();
// 라인 이동 Alt+ 방향키
// 복사 ctrl+Alt+방향키
// 지우기 ctrl+d
// 자동정렬
//ctrl + shift + f
//import ctrl+shift+o 여러개도 한번에 가능
Scanner sc = new Scanner(System.in);
//alt + shift +a 포인트 영역 설정
밥 먹고, 어제 못푼 문제를 열심히 풀어보자 !!!
할 수 있다.