[英]Value of variable not updating in Java NetBeans
I have two java files in same package.我在同一个 package 中有两个 java 文件。 I want to take the updated value of one variable from one file to another.
我想将一个变量的更新值从一个文件转移到另一个文件。 I wrote the following code.
我写了以下代码。 In class1.java:-
在class1.java:-
import javax.swing.JOptionPane;
public class class1 {
public static String bar = "Yes";
static int age = 26;
public static void main(String[] args){
switch(age) {
case 25: bar = "world";
break;
case 26: bar = "good";
break;
case 27: bar = "very";
break;
case 30: bar = "hello";
break;
default: JOptionPane.showMessageDialog(null,"Please");
break;
}
}
}
In class2.java:-在类2.java:-
public class class2 {
public static void main(String[] args){
class1 second = new class1();
System.out.println(second.bar);
}
}
The problem is that the final value is printed Yes which should not be printed.问题是最终值打印的是不应该打印的。 The output should be good .
output 应该不错。 Please help me.
请帮我。
class class1 {
public String getBar(String age){
String bar = "Yes";
switch(Integer.valueOf(age)) {
case 25: bar = "world";
break;
case 26: bar = "good";
break;
case 27: bar = "very";
break;
case 30: bar = "hello";
break;
}
return bar;
}
}
public class class2 {
public static void main(String[] args){
String age = JOptionPane.showInputDialog("Age Please");
class1 class1Obj = new class1();
System.out.println(class1Obj.getBar(age));
}
}
You create a class1
object, but you never run the main
method.您创建了一个
class1
object,但您从未运行main
方法。 This means that the section of code never runs, and thus bar
remains as "Yes"
.这意味着代码部分永远不会运行,因此
bar
保持为"Yes"
。
In class2
insert second.main(args);
在
class2
中插入second.main(args);
before you print second.bar
and you will get a good value.在你打印
second.bar
之前,你会得到一个很好的价值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.