2016-06-10 44 views
-1

如何訪問不帶類名稱的靜態變量?靜態變量總是用類名限定的,但在這種情況下,我可以使用它,而不需要類名。 怎麼可能?我們如何訪問不帶類名稱的靜態變量

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); 
} 


} 
+3

您在** **學生,所以它的暗示 – 2016-06-10 11:09:29

+0

這裏'主要'方法是'靜態' – emotionlessbananas

+0

下面你不能引用他們,因爲他們不存在靜態。您必須指定哪個實例具有它們:例如'st.email'。 – zapl

回答

3

靜態變量總是與類名

首先是合格的,這是不正確的,你有一個類名出線,你可以使用靜態導入,例如:

import static java.lang.Math.PI; 

接下來,您只需使用PI即可參考Math.PI。例如:

import static java.lang.Math.PI; 

public class Foo { 

    public static void main (String[] args) { 
     System.out.println(PI); 
    } 

}

更多信息可以查到here

接下來只要你是類的範圍內,所有的靜態成員都可以直接尋址而不需要限定。換句話說這個代碼片段:

public class Foo { 

    public static int static_member; 

    //within this scope you can call static_member without Foo. 

}
1

只有當您在課堂外引用它時,您才需要提供ClassName.staticMemberName

在你的情況,你重新在課堂上的靜態成員。

要回答你的第二個問題:

非靜態成員不能在靜態方法中直接使用。它 應該有對象引用。

所以,你的說法應該是像

System.out.println(st.email); 
+0

同樣的事情應該適用於非靜態變量。在我們創建對象後,非靜態變量被加載到內存中,並且我們在同一個類中,那麼我們可以直接訪問非靜態變量嗎? –

+0

這不是關於記憶。您可以在同一個main()中創建另一個Student對象。現在,如果你直接說電子郵件應該給java的對象電子郵件? –

+0

非常感謝Bhargav庫馬爾。由於實例變量可以多次使用,所以我們需要指定哪一個。 –

1

你可以調用靜態方法,無需在同一個班,你是在引用類,或通過使用靜態導入的。

public class Student { 
    public static void someStatic() { 
    } 

    public static void otherStatic() { 
     someStatic(); //works 
    } 
} 

另外:

import static Student.*; 
public class OtherClass { 
    public static void other() { 
     someStatic(); //calls someStatic in Student 
    } 
} 
1

這工作,因爲你是學生類的內部,它的隱含

public class Student { 
    public static final String SCHOOL ="Harvard"; 

    public static void main(String[] args) { 
     System.out.println(SCHOOL); 
    } 
} 

輸出:哈佛

public class Teacher { 
    public static final String SCHOOL ="Harvard"; 
} 

public class Student { 
    public static final String SCHOOL ="MIT"; 

    public static void main(String[] args) { 
     System.out.println(SCHOOL); 
     System.out.println(Teacher.SCHOOL); 
    } 
} 

輸出:MIT

輸出:哈佛

這也展示了爲什麼這項工作,因爲現在我們可以打印既有學校財產的老師和學生。

你問題的第二部分:

你不能叫電子郵件直接,因爲您的主要方法是靜態的。所以你不僅需要創建新的學生對象,而且還要使用它。

public class Teacher { 
     public static final String SCHOOL ="Harvard"; 
     public String Email = "[email protected]"; 
    } 

    public class Student { 
     public static final String SCHOOL ="MIT"; 
     public String Email = "[email protected]"; 

     public static void main(String[] args) { 
      System.out.println(SCHOOL); 
      System.out.println(Teacher.SCHOOL); 

      Student student = new Student(); 
      System.out.println(student .Email); 

      Teacher teacher = new Teacher(); 
      System.out.println(teacher.Email); 
     } 
    } 

輸出:MIT

輸出:哈佛

輸出:[email protected]

輸出:[email protected]