Results 1 to 3 of 3
  1. #1
    jruland is offline Member
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Default cannot set variable

    i have the following code:

    this.x -= a;

    however, after executing this code, this.x remains unchanged. i then changed the above code to the following:

    int b = this.x;
    b -= a;
    this.x = b;

    which of course doesn't work either. i stepped through this code many times with the same result: b changes, but the variable this.x remains unchanged. for example, i had a situation in which this.x was 150 and the variable a was 3. after running the code, this.x was still 150 when it should have been 147 (and b was 147). no exceptions are thrown, nothing. it's like it just skips the line where this.x is assigned (though using the debugger i was able to confirm that this is not the case).

    does anyone know what could be going on here? i have never seen this happen before (and i've been using java for about 7 years).
    Last edited by jruland; 03-19-2010 at 12:16 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Your problem isn't evident to me based on the code you have posted. You may wish to create a Short, Self Contained, Correct (Compilable), Example or SSCCE. This is a small program that compiles, runs, demonstrates your problem, and contains no extraneous code. Please read the link, and have a go at it. Creating an SSCCE actually serves two purposes, the most obvious being that you can post a short program that we can inspect and run, and help you solve your problem, but the better purpose is that often when you go through this process and are required to isolate your problem and inspect it in all its nakedness, you will experience an "aha" moment and will be able to solve the problem yourself. Best of luck.

  3. #3
    jruland is offline Member
    Join Date
    Mar 2010
    Posts
    5
    Rep Power
    0

    Default

    whoops. i figured it out. and yeah i did have an "ahah!" moment.

Similar Threads

  1. Your variable is never read
    By singularity in forum New To Java
    Replies: 4
    Last Post: 09-10-2009, 01:13 PM
  2. getting the value of variable
    By Lehane_9 in forum New To Java
    Replies: 2
    Last Post: 03-05-2008, 02:42 AM
  3. Getting variable value from a variable name
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-16-2008, 10:26 PM
  4. Variable issues!
    By Alex89 in forum New To Java
    Replies: 1
    Last Post: 11-07-2007, 08:51 PM
  5. Variable No. of Arguments
    By Gajesh Tripathi in forum New To Java
    Replies: 2
    Last Post: 10-31-2007, 03:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •