Results 1 to 5 of 5
  1. #1
    casta is offline Member
    Join Date
    Apr 2016
    Posts
    27
    Rep Power
    0

    Default Variable values change inexplicably

    Hi guys,

    I am having an issue here that I have not been able to resolve myself. I found that some variable values change without any method changing them.
    The following lines are below each other:
    Java Code:
    		for (Employee D: listOfEmployees)
    		{
    			D.setEmployeeDepartment(D, listOfDepartments);
    			System.out.println("HHH" + D.getEmployeeDepartment().hashCode());		
    		}
    		for (Employee D: listOfEmployees)
    		{
    			System.out.println("GGG" + D.getEmployeeDepartment().hashCode());
    		}
    The output is as follows:
    HHH1311053135
    HHH2018699554
    HHH1829164700
    HHH118352462
    HHH118352462
    HHH1311053135
    HHH2018699554
    HHH1311053135
    GGG1311053135
    GGG1311053135
    GGG1311053135
    GGG1311053135
    GGG1311053135
    GGG1311053135
    GGG1311053135

    What could be reasons of these changing variables?

    The code for the Employee class, setting and getting Department:
    Java Code:
    	public void setEmployeeDepartment(Employee D, List<Department> listOfDepartments) {
    
    		
    		for (int i = 0; i < listOfDepartments.size(); i++) {
    			   if( listOfDepartments.get(i).DepartmentName.equals(D.DepartmentInput))
    			   {
    				   EmployeeDepartment = listOfDepartments.get(i);
    			   }
    			}
    	
    		
    	}
    
    	public Department getEmployeeDepartment() {
    		return EmployeeDepartment;
    	}
    Departments are matched to Employees based on information that is read about these specific employees in variable DepartmentInput.
    Last edited by casta; 07-13-2016 at 11:57 AM.

  2. #2
    casta is offline Member
    Join Date
    Apr 2016
    Posts
    27
    Rep Power
    0

    Default Re: Variable values change inexplicably

    I just found that my employeeDepartment variable was static. This topic can be closed!

  3. #3
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: Variable values change inexplicably

    Don't you just hate it when that happens: searching for hours, asking in a forum and just after you pressed "submit", you find the answer :)
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  4. #4
    casta is offline Member
    Join Date
    Apr 2016
    Posts
    27
    Rep Power
    0

    Default Re: Variable values change inexplicably

    Haha, yes indeed. I was able to narrow it down to this specific point, but was not experienced enough to find easily that this would be the issue then. Well, too bad. I am very glad that these forums are there to help me in cases I would never find the solution ;)

  5. #5
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: Variable values change inexplicably

    Besides the usual "constants" as public static final String FOO = "bar" and its brethren, static variables have very specific use. I can say that about 99.9% of the variables in my project are non-static. If you have static variables/fields that are mutable, you must have a very good reason to do so. If you can't think of that reason, they probably shouldn't be static.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

Similar Threads

  1. change variable value from another class?
    By EStrike in forum New To Java
    Replies: 13
    Last Post: 01-02-2012, 08:32 PM
  2. Change a variable outside TimerTask after it is done?
    By a_programmer in forum Advanced Java
    Replies: 3
    Last Post: 07-20-2011, 03:04 AM
  3. Replies: 1
    Last Post: 04-17-2011, 03:59 PM
  4. Values change when they're not supposed to
    By m00nchile in forum New To Java
    Replies: 7
    Last Post: 05-15-2010, 04:16 PM
  5. Get all values of variable that it can take
    By umarsharief in forum New To Java
    Replies: 9
    Last Post: 01-20-2009, 02:35 AM

Posting Permissions

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