【发布时间】:2013-08-19 01:23:30
【问题描述】:
我有一个问题,无法解决。
我在 Action 类的方法 setDataAction(int a) 中将整数 a 分配给成员 dataAction。
分配后的值仍然不同! a == -3,dataAction == 0。 Eclipse 和 Java 均未引发错误或投诉。
环境: Win7-64 与 Eclipse Juno 和 JDK-7u25-win-64。
为了防止对这个方向发表任何评论,分配是在类的一个实例上完成的。我已经在几台机器上尝试过,甚至设置了一台绝对没有病毒,只有操作系统和环境的原始机器!
调试器屏幕截图的更好图片也可以在以下位置获得: http://setosix.eu/Java_Assignment_Problem.jpg/.gif/.png
// Class to hold return values of the applet to determine further action
class Action implements Constants {
int dataAction;
int appletAction;
int dataResult;
Action() {
dataAction = MOVE_NOT;
appletAction = ACT_CONTINUE;
dataResult = LV_OK;
}
public void setDataAction(int a) {
dataAction = a;
}
}
// here the 'Action' instance is created
public class TableResource extends Database {
private Connection dbLink;
private Statement stmt;
protected ResultSet rs;
// hold the return values of applet
protected Action nextAction;
protected TableResource() {
dbLink = getInstance().getConnection();
rs = null;
stmt = null;
nextAction = new Action();
}
...
// data class for 'AppletMandanten'
public class DataMandanten extends TableResource implements Data {
...
// constants are defined here
public interface Constants {
// constants defining moves in record-/browse-mode
public static final int MOVE_NOT = 0;
public static final int MOVE_FIRST = -1;
public static final int MOVE_LAST = -2;
public static final int MOVE_NEXT = -3;
public static final int MOVE_PREVIOUS = -4;
public static final int MOVE_NEXTPAGE = -5;
public static final int MOVE_PREVPAGE = -6;
...
// interface ‘Data’ extends interface ‘Constants’
public interface Data extends Constants {
...
// in this class the instance ‘data’ is creates
public class ModulMandanten extends EventHandler implements Data {
...
// main control of applet
public int control() {
int leave = LV_OK;
DataMandanten data;
// connect to database
db = Database.getInstance();
if( db.dbConnect() < 0 ) {
Intelligence.out( 1, "ERROR in ("+"RWG"+"): Failed to connect to Server!");
leave = LV_ERROR;
}
// here ‘data’ instance is created
data = new DataMandanten();
...
public abstract class Applet extends ModulMandanten {
...
// here the invocation takes place
public class AppletMandanten extends Applet {
...
// handle events in class ‘AppleMandanten’ (derives from ‘ActionPerformed()’
private void eventHandler( ActionEvent event ) {
...
// invocation is called in method 'eventHandler', class 'AppletMandanten'
switch (eventName){
case ("btnNext"): {
// 'next' button pressed
data.nextAction.setDataAction( MOVE_NEXT );
// alternatively: doesn't work neither
data.nextAction.setDataAction = MOVE_NEXT;
// 'data.nextAction.setDataAction' printed to console ( != MOVE_NEXT )
System.out.println(Integer.toString(data.nextAction.dataAction));
break;
// !!! after this 'dataAction' isn't touched again !!!
}
...
Java assignment issue in Eclipse debug mode http://setosix.eu/Java_Assignment_Problem.jpg
【问题讨论】:
-
告诉我们你是怎么打电话给
setDataAction的。你是在同一个实例上调用它吗? -
还向我们展示您如何测试
dataAction没有改变。 -
我们能看到对象的创建和setDataAction()方法的调用吗?可能是您没有在您认为的实例上调用该方法吗?我编译了你的类,并调用:
public class Test { public static void main(String[] args) { Action a = new Action(); a.setDataAction(12); System.out.println(a.dataAction); } }输出:java Test 12 -
原始 int 实例变量在实例创建时初始化为 0,因此如果您尝试为其分配其他内容但仍然为 0,则您的分配(或很可能 - 分配前的代码)必须具有失败并抛出异常,因此对 -3 的赋值从未发生。
-
为了测试'dataAction'没有改变,我通过调试器和'System.out.prinln(Integer.toString(dataAction));'测试了它。在赋值语句下方的方法“setDataAction(int a)”中。
标签: java eclipse variable-assignment