2013-04-14 89 views
5

我需要在其他類中獲取變量inString。我怎樣才能做到這一點?在其他類中獲取變量

public class main { 
    public static StringBuffer inString; 


    public static void main(String[] args) { 
     inString = new StringBuffer("Our aim is to make a 15 realistic game, where grinding powerlines and doing a tailwhip isn't easy, like in the real world. A game in which you will feel like you're actual riding. A game in which creativity and beauty are combined. "); 
     inString = new StringBuffer(inString.toString().replaceAll(" +", " ")); 
     } 
} 

因此,我想在我的Textcl.class使用System.out.println(main.inString);,但得到null

+1

因爲你從來沒有初始化它在其他靜態無效主要你在那裏運行 – Yoda

回答

3

您將得到null,因爲inString永遠不會被初始化,正如Robert Kilar在評論中正確指出的那樣。

通過使用類名引用靜態變量。

示例ClassName.variablename。在你的情況下

main.inString 

運行你的主類。當你運行inString在類的構造函數中被初始化。現在你可以在Myclass中引用同樣的內容。

public class main { 
public static StringBuffer inString; 

public main() 
{ 
inString = new StringBuffer("Our aim is to make a 15 realistic game, where grinding powerlines and doing a tailwhip isn't easy, like in the real world. A game in which you will feel like you're actual riding. A game in which creativity and beauty are combined. "); 
inString = new StringBuffer(inString.toString().replaceAll(" +", " ")); 
new MyClass(); 
} 
public static void main(String[] args) { 
    new main(); 
    } 
} 

現在在MyClass中引用靜態變量。

class MyClass { 

public MyClass() { 
    System.out.println("............."+main.inString);// refer to static variable 
} 
} 

您也可以將變量傳遞給類的構造函數。

public class main { 
public StringBuffer inString; 

public main() 
    { 
    inString = new StringBuffer("Our aim is to make a 15 realistic game, where grinding powerlines and doing a tailwhip isn't easy, like in the real world. A game in which you will feel like you're actual riding. A game in which creativity and beauty are combined. "); 
    inString = new StringBuffer(inString.toString().replaceAll(" +", " ")); 
    new MyClass(inString); 
} 
public static void main(String[] args) { 
    new main(); 

    } 
} 

然後在MYCLASS

public class MyClass 
{ 
     public MyClass(StringBuffer value) 
     { 
      System.out.println("............."+value); 
     } 
} 

請檢查鏈接@Why are static variables considered evil?

4

您可以通過:main.inString其中main是聲明變量public static的類的名稱。

+0

不要忘記'main.inString'中的大寫S – xagyg

+0

試試這個,但無法獲得變量( – antoxa2584

2

既然你在課堂上進行的現場靜,您使用類名來訪問它,即

main.inString 
1

使用JavaBeans並作爲它的一個領域存儲和爲此使用getter和setter。

JavaBeans是具有屬性的Java類。將屬性視爲私有實例變量。由於他們是私人的,所以他們可以從課外訪問的唯一途徑就是通過課堂中的方法。改變屬性值的方法稱爲setter方法,檢索屬性值的方法稱爲getter方法。

public class VariableStorage implements Serializable { 

    private String inString; 

    public String getInString() { 
     return inString; 
    } 

    public void setInString(String inString) { 
     this.inString = inString; 
    } 
} 

通過設置在郵件法的變量:

VariableStorage variableStorage = new VariableStorage(); 
variableStorage.setInString(inString); 

然後使用對象serialzation序列化此對象,並在你的其他類反序列化這個對象。

在序列化的對象可被表示爲字節序列,包括該對象的數據的信息以及關於該對象的類型和類型存儲在對象數據。

將序列化對象寫入文件後,可以從文件中讀取並反序列化。也就是說,表示對象及其數據的類型信息和字節可用於在內存中重新創建對象。

如果您需要此教程,請參閱Serialization in Java