Static
package ex6_static;
public class Number {
static int num = 0;
int num2 = 0;
public static void main(String[] args) {
Number number1 = new Number();
Number number2 = new Number();
number1.num++; //static 변수 num 1증가
number1.num2++; //member 변수 num2 1증가
System.out.println(number2.num);
System.out.println(number2.num2);
}
}
Static 예제
package ex6_static;
public class Bank {
private String name = "우리은행";
private String point; //은행 위치
private String tel; //은행 번호
static float interest = 10f; //은행 이자
//setter 대신 생성자로 값 대입 가능
public Bank(String point, String tel) {
this.point = point;
this.tel = tel;
}
public void printBank() {
System.out.println("이름 : "+name);
System.out.println("위치 : "+point);
System.out.println("번호 : "+tel);
System.out.println("이자율 : "+interest);
System.out.println();
}
}
package ex6_static;
public class BankMain {
public static void main(String[] args) {
Bank bank1 = new Bank("강남","02-222-2222");
Bank bank2 = new Bank("인천","032-333-3333");
Bank bank3 = new Bank("대전","042-444-4444");
//static 변수는 객체 생성 없이 사용 가능
Bank.interest = 0.1f;
bank1.printBank();
bank2.printBank();
bank3.printBank();
}
}