저는 초보자이며 의 개념을 이해하기 위해 Java를 배우려고 구입 한 책에서을 구입하려고합니다. 그러나, 프로그램에서의 나의 실험 때문에, 나는 매우 혼란 스럽다. 이 책에서는 static이이 키워드를 참조 할 수 없다고 말합니다. 정적이 아닌 일반 함수를 사용하기 시작할 때까지는 괜찮습니다. 이것에서, 나는이 키워드로 정적 변수에 접근 할 수 있었다 !!왜 자바에서 정적 변수와 함께이 키워드를 사용할 수 있습니까?
import java.util.Random;
class Static {
public static void main(String[] com){
System.out.println("By default the mobile is having "
+ Mobile.RAM + "gigabytes of RAM and"
+ Mobile.CameraMP + " mega pixels of camera");
Mobile S4mini = new Mobile("S4 Mini" , 4 , 16);
S4mini.displayMobileSpecs(true);
Mobile mob2 = new Mobile("fdf" , 23 , 45);
mob2.displayMobileSpecs(true);
S4mini.displayMobileSpecs(false);
}
}
class Mobile{
static int RAM;
static int CameraMP;
Random rand = new Random();
double InternalMemorySpace;
double ExternalMemorySpace;
String modelNo;
Mobile(String modelName,double internalMem , double externalMem)
{
this.modelNo = modelName;
this.InternalMemorySpace = internalMem;
this.ExternalMemorySpace = externalMem;
}
static {
RAM = 4;
CameraMP = 12;
System.out.println("The Static part of the class is executed");
}
void displayMobileSpecs(boolean change){
if(change)
this.RAM = (int) rand.nextInt(8) + 2;
System.out.println(this.RAM + " , " + Mobile.RAM);
}
}
그래서 정적 변수 이것을 사용하여 액세스 할 수있다 아닌 정적 또는 기능에 어떤 (다음의 코드의 기능 displayMobileSpecs 참조) ?? 나는 정말로 혼란스럽고 이것은 아주 어리석은 질문이다. 그러나 나에게 대답 해주세요.
(참고 :. 그런데, 프로그램에서 사용하는 모바일의 말도 예를 신경 쓰지 마십시오 감사합니다 :))
'this'와 같은 인스턴스 참조를 통해 정적 멤버를 참조 할 수 있지만 그렇게해서는 안됩니다. –
나는 동의한다.. 그러나 나의 대답은 영업 질문에 한해지고 있었다, 당신은 그것을 가져 가고 있고, donts한다. 추가 해줘서 고마워. – mhasan