2013-01-01 55 views
1

由於某些原因,我添加並從代碼中移除了一個靜態可用的項目後,eclipse開始給我調用所有函數的錯誤,並說這些函數必須是靜態的。但是,如果我讓程序運行這些錯誤,程序就像我打算的那樣運行。我的代碼:Eclipse強制所有東西都是靜態的

package main; 

public class Main implements Runnable { 

    public void start() { 

     Thread thread = new Thread(this); 
     thread.start(); 
     System.out.println("Running..."); 

     Ball.test(); <--- Giving me an error 

    } 

    public void run() { 

    } 

    public void stop() { 
     System.out.println("Exiting..."); 
    } 

} 

,當我創造球名爲test的方法它給了我:

public static void test() { 
    // TODO Auto-generated method stub 
} 

回答

2

嗯,是的 - 你調用該方法,好像它是一個靜態方法:

Ball.test() 

如果您想調用實例方法,您需要在實例上調用它,例如

Ball ball = new Ball(); 
ball.test(); 

瞭解靜態成員和實例成員之間的區別很重要。你讀過appropriate chapter of the Java tutorial嗎?你有一本好書可以幫助你嗎? (Stack Overflow對於特定的問題很好,但對於從頭開始學習語言不是很好,解釋語言概念需要很多空間和時間。)

+0

我是個白癡,我怎麼會忘記它!非常感謝 –

相關問題