
package com.haenin.section07.initblock;
public class Application {
public static void main(String[] args) {
Product product = new Product();
System.out.println(product);
}
}
package com.haenin.section07.initblock;
public class Product {
private String name = "아이폰"; // 인스턴스 변수 1
private int price; // 인스턴스 변수 2
private static String brand; // 클래스 변수
/* 필기. init블록 생성자는 다중 1 선택
* 알고리즘...을 입력
* 생성자로 생성되기 전에 항상 실행되는 init 블록*/
/* 필기. 어떤 생성자를 쓰든 값을 동일하게... */
// 필기. 필드와 생성자 사이에 로직은 작성이 안됌 name = "엘지"
/* 설명. 초기화 블럭은 생성자 이전에 실행되며 어떤 생성자로 생성하든
* 공통적인 로직이 있다면 작성*/
{
System.out.println("초기화 블록 실행 ...");
name="폴드";
price= 100;
brand = "삼성";
}
static {
// price = 200; 필기. static init 블록에서 인스턴스 변수(non-static)에 접근이 불가능하다.
brand = "현대";
}
public Product(){
System.out.println("Product 기본 생성자 호출됨...");
brand = "퓨리오사AI";
}
@Override
public String toString() {
return "Product{" +
"neame='" + name + '\\'' +
", price=" + price +
", brand='" + brand + '\\'' +
'}';
}
}