JAVA chapter 06. 클래스. 6.9 인스턴스 멤버와 this. 6.10 정적 멤버와 static
JAVA/CONCEPT 2017. 11. 18. 19:12 |6.9 인스턴스 멤버와 this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | public class Car { //필드 String model; int speed; //생성자 Car(String model) { this.model = model; } //메소드 void setSpeed(int speed) { this.speed = speed; } void run() { for(int i=10; i<=50; i+=10) { this.setSpeed(i); System.out.println(this.model + "가 달립니다.(시속:" + this.speed + "km/h)"); } } } |
1 2 3 4 5 6 7 8 9 | public class CarExample { public static void main(String[] args) { Car myCar = new Car("포르쉐"); Car yourCar = new Car("벤츠"); myCar.run(); yourCar.run(); } } | cs |
6.10 정적 멤버와 static
6.10.1 정적 멤버 선언
6.10.2 정적 멤버 사용
//
(1) 클래스 이름으로 접근해야 하지만,
(2) 객체를 먼저 생성하고 참조 변수(객체 참조 변수)로 접근이 가능하다. (클래스 이름으로 접근하기.. 아니면 오류 생길 수 있음)
(2) 객체 참조 변수로 접근
1 2 3 4 | Calculator myCalcu = new Calculator(); double result1 = 10 * 10 myCalcu.pi; int result2 = myCalcu.plus(10,5); int result3 = myCalcu.minus(10,5); | cs |
(1) 클래스 이름으로 접근
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | package sec10.exam01_static_member; public class CalculatorExample { public static void main(String[] args) { double result1 = 10 * 10 * Calculator.pi; int result2 = Calculator.plus(10, 5); int result3 = Calculator.minus(10, 5); System.out.println("result1 : " + result1); System.out.println("result2 : " + result2); System.out.println("result3 : " + result3); } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 | package sec10.exam01_static_member; public class Calculator { static double pi = 3.14159; static int plus(int x, int y) { return x + y; } static int minus(int x, int y) { return x - y; } } | cs |
6.10.3 정적 초기화 블록
// 자바는 정적 필드의 복잡한 초기화 작업을 위해서 정적 블록을 제공한다
1 2 3 4 5 6 7 8 9 10 11 | package sec10.exam02_static_block; public class Television { static String company = "Samsung"; static String model = "LCD"; static String info; static { info = company + "-" + model; } } | cs |
1 2 3 4 5 6 7 | package sec10.exam02_static_block; public class TelevisionExample { public static void main(String[] args) { System.out.println(Television.info); } } | cs |
6.10.4 정적 메소드와 정적 블록 작성시 주의할 점
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | package sec10.exam03_static_be_careful; public class Car { int speed; void run() { System.out.println(speed + "으로 달립니다."); } public static void main(String[] args) { Car myCar = new Car(); myCar.speed = 60; myCar.run(); } } | cs |
6.10.5 싱글톤(Singleton)
싱글톤
1 2 3 4 5 6 7 8 9 10 11 | package sec10.exam04_singleton; public class Singleton { private static Singleton singleton = new Singleton(); private Singleton() {} static Singleton getInstance() { return singleton; } } | cs |
싱글톤 객체
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | package sec10.exam04_singleton; public class SingletonExample { public static void main(String[] args) { /* 생성자 호출 -> 객체 생성 Singleton obj1 = new Singleton(); //컴파일 에러 (private때문에 클래스 외부에서 new 생성자로 호출 X) Singleton obj2 = new Singleton(); //컴파일 에러 (싱글톤은 하나의 객체이고, 생성자를 호출한만큼 객체가 생성 되므로 X) */ Singleton obj1 = Singleton.getInstance(); Singleton obj2 = Singleton.getInstance(); if(obj1 == obj2) { System.out.println("같은 Singleton 객체 입니다."); } else { System.out.println("다른 Singleton 객체 입니다."); } } } | cs |
'JAVA > CONCEPT' 카테고리의 다른 글
JAVA chapter11. 기본 API 클래스. 11.15 Format 클래스 (0) | 2017.11.27 |
---|---|
JAVA chapter11. 기본 API 클래스. 11.14 Date, Calendat 클래스 (0) | 2017.11.27 |
JAVA chapter 06. 클래스. 6.5 클래스의 구성 멤버. 6.6 필드. 6.7 생성자 (0) | 2017.11.18 |
JAVA chapter 06. 클래스. 6.4 객체 생성과 클래스 변수 (0) | 2017.11.18 |
JAVA chapter 06. 클래스. 6.2 객체와 클래스. 6.3 클래스 선언 (0) | 2017.11.18 |