2015-06-25 34 views
0

我真的不明白我的程序正在發生什麼。我調用[另一個類]的方法,並以某種方式[調用該方法的類]停止了調用的地方,並且不執行程序的其餘部分。從另一個班級調用方法停在該語句

沒有錯誤彈出或者,它只是簡單地didnt運行,其餘(經過與打印語句)

這裏是我的代碼:

類調​​用該方法

Banana ban = new Banana(); 
String[] listOfBananas = ban.getBananas(); //Stops at this statement 
//Below is a check that never gets executed as with the rest of the program 
System.out.println("I didnt get to this statement"); 

//Do other stuff with the array... 

有方法的類

public class Banana { 
    public static String[] giveOtherClassList; 

    public Banana(){ 
    } 

    public static void main(String[] args) { 
     StringBuilder a = new StringBuilder(); 
     a.append("text text1 text2 text3"); 
     giveOtherClassList = a.toString().split(" "); //Split will create an array 
    } 

    public String[] getBananas() { 
     //I know this method works because I ran this program with 
     //giveOtherClassList[3] and it returned the correct value 
     return giveOtherClassList; 
    } 
} 
+1

你可能已經用'檢查打印語句「...但你有沒有'調試'? –

+3

難道你是在'Banana'中執行'main'方法而不是你想要執行的'main'方法嗎?而且,你有一些語法錯誤:Bananas ban = ...''Banana ban = ...'和'giveOtherclassList'聲明在一個類的外部) – Turing85

+1

giveOtherClassList數組在聲明類Banana之外聲明? – John

回答

0

根據您提供的代碼,我確定Banana中的main方法得到執行,而不是您實際想要執行的代碼。只需在Banana內刪除main方法並重新運行測試。可以肯定的是:在您的main中添加System.out.println("Start");作爲第一行。

這裏只是用於文檔目的MWE:

public class Main implements Foo { 
    public static void main(String... args) { 
     Banana ban = new Banana(); 
     String[] listOfBananas = ban.getBananas(); 
     System.out.println("I didnt get to this statement"); 
    } 
} 

class Banana { 
    public static String[] giveOtherClassList; 

    public Banana() { 
    } 

    public String[] getBananas() { 
     return giveOtherClassList; 
    } 
} 

該程序生成的輸出I didnt get to this statement,其中 - 明確 - 是不正確的,因此程序工作:)

+0

我有3個類:一個類使用主方法調用第一個代碼,第二個使用該數組,第三個從服務器創建數組。在香蕉的主要是測試連接到服務器,並確保它正在接收數據放入一個數組 –

+0

創建一個新的香蕉,並獲得香蕉的名單實際上是另一種方法,從一個叫從猴子類; 因此,主要方法是在猴類中,但是,您的解決方案可能工作。謝謝! –

+0

@布萊恩,這並不重要。你的核心問題是執行錯誤的「主要」方法。 – Turing85

相關問題