💻 Java initialized
🎯 목표 : 변수별 초기화 방법 이해
📒 초기화
지역 변수에서의 초기화란 변수를 선언 후 최초의 값을 input하는 걸 의미한다
클래스,인스턴스 변수에서의 초기화는 컴파일러가 자동으로 해주며, 각 데이터 타입별 default 값을 가지게된다.
📒 지역변수의 초기화
📌 메소드 내에서 선언된 변수는 호출 하기 전에 수동으로 초기화를 해야된다.
👉 지역변수의 초기화 예제
public class InitializedStudy {
public static void main(String[] args) {
int e;
String f;
boolean g;
char h;
System.out.println(e); //Error
System.out.println(f); //Error
System.out.println(g); //Error
System.out.println(h); //Error
}
}
- main 메소드 내에서 int,String,boolean,char 각 타입별 지역 변수를 선언 하였다.
- 지역변수를 println으로 호출하게되면 위 사진과 같이 "초기화되지 않았을 수 있습니다" 라는 에러가 발생하게 된다.
public class InitializedStudy {
public static void main(String[] args) {
int e = 1;
String f = "a";
boolean g = true;
char h = 'A';
System.out.println(e);
System.out.println(f);
System.out.println(g);
System.out.println(h);
}
}
//=====출력값 =====
// 1
// a
// true
// A
- 위와 같이 main 메소드 내에서 지역변수의 값을 선언과 동시에 초기화 하면 정상적으로 출력하는걸 확인할수 있다.
📒 멤버변수 초기화
📌 Class 변수와 Instance 변수는 선언하는 순간 default값으로 초기화 된다.
📌 Class 변수는 로딩될때, Instance 변수는 객체가 생성될때 자동으로 default값을 초기화 해준다.
👉 멤버변수 초기화 예제
class Init {
int a;
String b;
boolean c;
char d;
static int w;
static String x;
static boolean y;
static char z;
}
public class InitializedStudy {
public static void main(String[] args) {
System.out.println(Init.w);
System.out.println(Init.x);
System.out.println(Init.y);
System.out.println(Init.z);
Init in = new Init();
System.out.println(in.a);
System.out.println(in.b);
System.out.println(in.c);
System.out.println(in.d);
}
}
//===== 출력값 =====
// 0
// null
// false
//
// 0
// null
// false
//
- 멤버 변수 모두 선언만 하고 값을 정의하지 않았는데 main메소드에서 호출하여 출력하면 각 데이터 타입 별 default 값을 출력하는것을 볼수있다.
- Class 변수를 원하는 값으로 초기화 하는방법은 선언과 동시에 값을 정의해주거나, "static {값}" 블록을 활용하여 초기화 가능하다.
static int w = 1;
static {x = "init";}
- Instance 변수를 원하는 값으로 초기화 하는 방법은 선언과 동시에 값을 정의해주거나, 객체 생성후 값을 정의해주거나, "{값}" 블록을 사용하거나, 생성자를 통하여 원하는 값으로 초기화 할수 있다.
class Init {
int a =7; // 변수 선언과 동시에 초기화
String b; // 인스턴스 변수 초기화 블럭으로 초기화
{ b="init"; }
boolean c; // 생성자 초기화
Init (){
c = true;
}
}
public class InitializedStudy {
public static void main(String[] args) {
Init in = new Init(); // 객체 생성 후 초기화
in.d = 'B';
}
}
- Instance 변수 초기화 블럭("{ }")은 잘 사용되지 않으나, 생성자 보다 먼저 실행된다.
👉 멤버변수 초기화 시점 예제
Class 변수와 Instance 변수가 초기화 되는 시점 확인
class Test {
static int t1 = 1; // Class 변수
int t2; // Instance 변수
static { // Class 변수의 초기화
t1 = 15;
}
Test(int t2) { // Instance 변수 생성자 초기화
this.t2 = t2;
}
}
public class resetStudy {
public static void main(String[] args) {
Test a1 = new Test(25);
System.out.println(a1.t2);
}
}
// ===== 출력값 =====
// 25
- 클래스 생성과 동시에 로딩된다.
- 클래스 변수가 default 값으로 초기화 된다 => 0
- 클래스 변수가 연산자로 인하여 재 초기화 된다 => 1
- static{} 블록으로 클래스 변수가 재 초기화 된다 => 15
클래스 로딩후 한번만 초기화 되고 이후에 객체를 재 생성하더라도 초기화는 자동으로 되어있다. - 인스턴스 변수가 default 값으로 초기화 된다 => 0
- 인스턴스 변수가 객체 생성후 생성자에 의해 초기화 된다. => t2
- 인스턴스 변수가 매개변수에 의해 초기화 된다 => 25
'Language > JAVA' 카테고리의 다른 글
Java Overriding(오버라이딩) - OOP_상속 (0) | 2022.08.21 |
---|---|
Java Class의 Inheritance(상속) / Composite(포함) - OOP_상속 (0) | 2022.08.20 |
Java this(생성자, 참조변수)-OOP (0) | 2022.08.20 |
Java 생성자(Constructor)- OOP (0) | 2022.08.20 |
Java 기본형 / 참조형 매개변수 (0) | 2022.08.20 |