从另一个 class 调用方法在该语句处停止
Calling a method from another class stops at that statement
我不太明白我的程序发生了什么。我从 [另一个 class] 调用了一个方法,但不知何故 [调用该方法的 class 在进行调用的地方停止工作并且不执行程序的其余部分。
也没有弹出错误,它只是没有 运行 其余的(用打印语句检查)
这是我的代码:
Class 调用方法
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...
Class 有方法
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;
}
}
鉴于您提供的代码,我很确定 Banana
中的 main
方法被执行,而不是您实际想要执行的代码。只需删除 Banana
中的 main
方法并重新运行测试。可以肯定的是:将 System.out.println("Start");
添加为 main
.
中的第一行
这是一个仅用于文档目的的 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
,这显然是不正确的,因此这个程序可以工作:)
我不太明白我的程序发生了什么。我从 [另一个 class] 调用了一个方法,但不知何故 [调用该方法的 class 在进行调用的地方停止工作并且不执行程序的其余部分。
也没有弹出错误,它只是没有 运行 其余的(用打印语句检查)
这是我的代码:
Class 调用方法
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...
Class 有方法
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;
}
}
鉴于您提供的代码,我很确定 Banana
中的 main
方法被执行,而不是您实际想要执行的代码。只需删除 Banana
中的 main
方法并重新运行测试。可以肯定的是:将 System.out.println("Start");
添加为 main
.
这是一个仅用于文档目的的 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
,这显然是不正确的,因此这个程序可以工作:)