01. Java (v)
02. git
03. Database
04. Jsp [Server]
05. 미니프로젝트 (3W)
06. HTML,CSS
07. JS
08. SpringFramework , SrpingBoot
09. React JS [Front-end]
10. 중간프로젝트 (1M)
11. Linux 명령어
12. AWS 클라우드
13. DevOps - Docker
14. App - Android
15. 최종프로젝트 (1M)
static {
System.out.println("Static block is executed.");
staticVariable = 10; // static 변수 초기화
}
public static void main(String args[]){
System.out.println("Main method is executed.");
System.out.println(staticVariable);
}
// 실행결과
>> Static block is exectued.
>> Main method is executed.
>> 10
public static void main(String args[]){
Count c1 = new Count();
c1.a++;
c1.b++;
System.out.println(c1.a); // non-static변수
System.out.println(c1.b); // static변수
//----------------------------------------
// 실행결과
// >> 1
// >> 1
// ----------------------------------------
Count c2 = new Count();
c2.a++;
c2.b++;
System.out.println(c2.a); // non-static 변수
System.out.println(c2.b); // static 변수
//----------------------------------------
// 실행결과
// >> 1
// >> 2 // 모든 객체들이 공유하는 공유변수
// ----------------------------------------
System.out.println(c1.b); // Count 클래스에 있는 static 변수 b
System.out.println(Count.b);
}
// static 패키지
public class Count{
public int a =0;
public static int b=0;
public static int doIt(){
// return ++a; // static메서드 안에서는 non-static 멤버를 객체 생성 없이 직접 참조할 수 없음
return ++b;
}
}
public class StaticMethodExample{
public static void main(String args[]){
System.out.println(count.doIt());
System.out.println(count.doIt());
System.out.println(count.doIt());
}
}
// 실행 결과
>> 1
>> 2
>> 3
public class StaticInit{
// static initializer
static{
System.out.println("static initializer가 수행됨");
}
public staticInit(){
System.out.println("constructor 호출");
}
}
public class StaticInitExample{
public static void main(String args[]){
StaticInit a1 = new StaticInit();
System.out.println(s1);
StaticInit a2 = new StaticInit();
System.out.println(s2);
System.out.println("main() 메서드 종료");
}
}
// 실행결과
// >> static initializer가 수행됨 // 정적 초기화자 실행
// >> Constructor 호출
// >> static_.StaticInit@주소1
// >> Constructor 호출
// >> static_.StaticConstructor@주소2
// >> main() 메서드 종료
static멤버는 객체 생성 없이 클래스명.이름 으로 참조 가능
static변수는 객체간 값의 공유를 의미
static메서드는 같은 static멤버만 참조가능 (클래스명.메서드명 으로 참조)
Math.random();
Arrays.toString(배열명);
Integer.parseInt(문자열);
public class Compnay{
private String str;
private static Company c = new Company(); // static 객체 생성
private Company(){
str= "company";
System.out.println(str);
}
// getter메서드를 이용해 객체반환
public static Company getCompany(){
return c;
}
}
public class SingletonExample{
public static void main(String args[]){
Company c1= Company.getCompany();
Company c2 =Company.getComapny();
System.out.println(c1==c2); // true
}
}
public static final long VERSION = 1L;