2014-12-30 43 views
0

嗨,大家好我是新來的Java我定義的變量爲全局並試圖從一個靜態函數訪問變量,如如下:錯誤而訪問全局變量,靜態函數

public class PDFEMAIL 
{ 
    String a = "abc"; 

    public static void main(String args[]) 
    { 
     testme(); 
    } 

    public static void testme() 
    { 
     System.out.print("Welcome "+a); 
    } 

}//class end 

我得到一個錯誤如下所示:

error: non-static variable a cannot be referenced from a static context 
               System.out.println("Welcome"+a); 

請幫幫忙。我需要把它作爲「Welcome abc」我是新來的!

+0

那麼,讓'a'爲'static'變量? –

回答

3

變量a也不是一成不變的,這就是爲什麼你不能從靜態方法訪問testMe() .Change

String a = "abc"; 

static String a = "abc"; 
+0

@感謝你的朋友! – Santhucool

+0

@Santhucool很高興幫助 – sol4me

3

a不是一個全局變量。如果你想要它是全球性的,請將其設爲static

+0

謝謝你哥們它工作! – Santhucool