如何訪問不帶類名稱的靜態變量?靜態變量總是用類名限定的,但在這種情況下,我可以使用它,而不需要類名。 怎麼可能?我們如何訪問不帶類名稱的靜態變量
class Student
{
String email;
String name;
long phone;
static String school="jlc";
public static void main(String[] args)
{
Student st= null;
System.out.println(school);//this should be Student.school but its working.
}
}
在創建學生對象之後下面的程序,變量已經加載到內存中,但我不能直接使用對象與進出它reference.but我們可以爲靜態的事情。
class Student
{
String email;
String name;
long phone;
static String school="jlc";
public static void main(String[] args)
{
Student st= new Student();
System.out.println(email);
}
}
您在** **學生,所以它的暗示 – 2016-06-10 11:09:29
這裏'主要'方法是'靜態' – emotionlessbananas
下面你不能引用他們,因爲他們不存在靜態。您必須指定哪個實例具有它們:例如'st.email'。 – zapl