static
mymain 패키지
-----------------
package mymain;
import myutil.Save;
public class MyMain_Save {
public static void main(String[] args) {
// TODO Auto-generated method stub
//이율지정
//Static변수의 사용방법 : 클래스명.변수명(객체가 생성되기 전에 이리 씀)
Save.save_rate = 0.1; //10%
Save s1 = new Save("일길동", 1000000);
Save s2 = new Save("이길동", 2000000);
Save s3 = new Save("삼길동", 3000000);
System.out.println("---이율 : 0.1--");
s1.display();
s2.display();
s3.display();
//객체생성후의 static변수의 표현
s1.save_rate = 0.2; //20% 왜 인스턴트변수로 이렇게 선언하느냐 클래스명.멤버변수로 사용하지
System.out.println("---이율 : 0.2--");
s1.display();
s2.display();
s3.display();
}
}
/*객체생성전
클래스명.멤버변수
객체생성후
참조변수.멤버변수*/
--------------
myutil
--------------
package myutil;
public class Save {
String name; //저축자명
int money; //저축액
//정적변수, 클래스변수
public static double save_rate; //이율
// Alt + Shift + S (밑에서 3번째)
// Overload생성자가 존재하면 기본생성자는 무조건 만들어라...
public Save() {
// TODO Auto-generated constructor stub
}
public Save(String name, int money) {
super();
this.name = name;
this.money = money;
}
public void display()
{ //이름, 저축액, 이율, 이자
System.out.printf("%-10s%10d%5.2f%10d\n",
this.name,
this.money,
save_rate,//클래스명.save_rate : Save.save_rate 실제론 클래스명 안붙임
(int)(this.money*save_rate)
);
}
}