Problem with weird java assignment

I have a problem and I can not get around it.

I assigned an integer a to the dataAction member in the setDataAction(int a) method of the Action class.

After assignment, the values ​​are still different! a == -3 , dataAction == 0 . No error or complaint was caused by either Eclipse or Java.

Environment: Win7-64 with Eclipse Juno and JDK-7u25-win-64.

To avoid comments in this direction, the assignment was performed on an instance of the class. I tried this on several machines and even created an untouched version with definitely no virus, only OS and Environment!

The best picture of a screenshot of the debugger is also available at: 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 !!! } ... 

Problem with Java Assignment in Eclipse Debug Mode http://setosix.eu/Java_Assignment_Problem.jpg

+4
source share
1 answer

Back to the basics. Java performs tasks correctly - no error can be expected in this part of Java. When you write x=a , and a is -3, then x will be immediately 3. Several things that I see in your code that may go wrong:

Multiple threads

Change your code with AtomicInteger and change the caller, so setting up and receiving while waiting for the same value is one atomic call to the AtomicInteger method, not separate method calls. synchronized will not work in the case of the screenshot above.

Track problem

Write a and dataAction before assignment. And if you want to be sure, right after the job again, but keep in mind that another thread could jump in the middle, as well as setDataAction() . I assume that you will find either some strange settings 0 or not call with -3.

Now that you see the wrong destination, add a little code to your setDataAction() .

 // add in setDataAction() if(a == the-wrong-value) try { throw new Exception("wrong assignment: " + a); } catch(Exception e) { e.printStackTrace(); } 

So, you will find out where the wrong assignment comes from, and you can keep track of what is actually happening.

+1
source

Source: https://habr.com/ru/post/1497457/


All Articles