Static and Instance

// Static 개념 ==> 정적인 접근(정해져있는 데이터나 로직, 고정적인 상수 등) ==> 클래스명.멤버
// 인스턴스  개념 ==> 인스턴스 접근(동적인 개념, 여러객체 생성 가능) ==> 객체.멤버
// 상품 클래스
class Product
{
	public static String modelName; // 상품명 저장 공간
	public static int unitPrice;    // 단가
	public static void print() {
		System.out.println(modelName +","+ unitPrice);
	}
}
// 고객 클래스
class Customer {
	public String name;
	public int age;
	public void print(){
	System.out.println(name +","+ age);
	}
}
public class StaticAndInstanceDemo {

	
	public static void main(String[] args) {
		//Product 클래스의 멤버에 접근하고자 하려면  ==> 클래스명.멤버명(정적인 접근)
		Product.modelName = "쉽게 배우는 자바 강의";
		Product.unitPrice = 33000;
		Product.print();

		// Customer 클래스의 멤버에 접근하고자 한다면?  ==> 객체.멤버명(객체 생성후 접근 가능)
		Customer cust1 = new Customer(); // 클래스의 인스턴스(객체) 생성
		cust1.name = "홍길동"; cust1.age= 21; cust1.print();
		
		Customer cust2 = new Customer();
		cust2.name = "백두산"; cust2.age= 100; cust2.print();
	}
}

'Java' 카테고리의 다른 글

상속(Inheritance)  (0) 2011.11.29
생성자  (0) 2011.11.29
멤버 변수, 상수  (0) 2011.11.29
클래스의 선언 및 호출방식  (0) 2011.11.29
재귀함수(팩토리얼 구하기)  (0) 2011.11.29