Results 1 to 10 of 10
  1. #1
    JimmyD is offline Member
    Join Date
    Oct 2011
    Location
    New Jersey
    Posts
    44
    Rep Power
    0

    Default How to update state variables in Objects (similar to pass by reference)

    Hi, I am doing some simulation and I wish to keep track on many data counter/state variable.

    The simulation looks sth like this.

    for sim loop 1 to 100
    { simulate(..., data) }

    Ideally, if I can pass data somehow by reference, at each simulation iteration, those state variables could be updated.

    Apparently the passing-by-value property of java prohibits this approach.

    I can think of ways to get around it, for example using text file i/o: output the state variable values to textfiles and update the file after every simulation.

    However I wish to ask for insights on a better/cleaner approach..

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: How to update state variables in Objects (similar to pass by reference)

    Make that simulate( ... ) method return the new data object? Something like this:

    Java Code:
    for sim loop 1 to 100 {
       Data newData= simulate( ..., data);
       if (newData != null)
          // set newData ...
    }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: How to update state variables in Objects (similar to pass by reference)

    Am I missing something here, but if you have a Data class then isn't your data passed into the simulate() method therefore a reference to the object?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: How to update state variables in Objects (similar to pass by reference)

    Quote Originally Posted by Tolls View Post
    Am I missing something here, but if you have a Data class then isn't your data passed into the simulate() method therefore a reference to the object?
    You're not missing anything but your approach fails if the state variable happens to be an enum (<--- a perfect type for states)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: How to update state variables in Objects (similar to pass by reference)

    But the OP says there are "many" states to keep track of, so I envisaged the Data object containing a bunch of these things...so enums or not they are still part of the overall reference.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: How to update state variables in Objects (similar to pass by reference)

    Quote Originally Posted by Tolls View Post
    But the OP says there are "many" states to keep track of, so I envisaged the Data object containing a bunch of these things...so enums or not they are still part of the overall reference.
    Yes, if the object can have many states at the same time, your solution is easier than a single enum that represents the Cartesian product of all the individual states.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    JimmyD is offline Member
    Join Date
    Oct 2011
    Location
    New Jersey
    Posts
    44
    Rep Power
    0

    Default Re: How to update state variables in Objects (similar to pass by reference)

    Hey guys, thanks a lot for the reply! That's very helpful indeed. Just 1 question to JosAH.. I have very little experience with enum in java.. why is it perfect for state variables? I mean.. what's its advantage over plain field variables / arrays?

    Well yesterday I did some more experiments.. looks like my original approach will work... I paste some simple test code.. Please feel free to comment=)



    test code:

    ================================
    public class TestData {

    public int A;

    public TestData()
    {
    A = 0;
    }

    public String toString()
    {
    return "" + A;
    }
    }
    =====================================

    public class DataUpdater1 {


    public DataUpdater1(TestData td)
    {
    td.A++;
    }


    }

    ==========================================
    public class DataUpdater2 {

    TestData lol;

    public DataUpdater2(TestData td)
    {
    lol = td;
    }

    public void increase()
    {
    lol.A++;
    }

    }

    ===========================================

    public class test{


    public static void main(String[] args)
    {
    TestData td1 = new TestData();
    TestData td2 = new TestData();

    System.out.println("td1 = " + td1);
    System.out.println("td2 = " + td2);

    for (int i = 0; i < 10; i++)
    {
    DataUpdater1 zz1 = new DataUpdater1(td1);
    DataUpdater2 zz2 = new DataUpdater2(td2);
    zz2.increase();
    System.out.println("td1 = " + td1);
    System.out.println("td2 = " + td2);
    }


    }

    }

    ================================ execution

    C:\Desktop\PassByValue>java test
    td1 = 0
    td2 = 0
    td1 = 1
    td2 = 1
    td1 = 2
    td2 = 2
    td1 = 3
    td2 = 3
    td1 = 4
    td2 = 4
    td1 = 5
    td2 = 5
    td1 = 6
    td2 = 6
    td1 = 7
    td2 = 7
    td1 = 8
    td2 = 8
    td1 = 9
    td2 = 9
    td1 = 10
    td2 = 10
    Last edited by JimmyD; 10-20-2011 at 05:44 PM.

  8. #8
    kennyman94 is offline Member
    Join Date
    Feb 2011
    Posts
    24
    Rep Power
    0

    Default Re: How to update state variables in Objects (similar to pass by reference)

    enums are useful because they only allow for certain, already defined variables. you can simulate an enum by having different public static final variables to represent one of the possible values for the state, however it is possible to pass a value that is NOT one of the defined values. In such a case, you would have to write detection code to detect when the state that is being used is not one of the valid values defined by the static final variables. In addition, you would have to make the method throw an exception. If it is a checked exception that you end up throwing, then end up forcing anything that uses it to account for the situation where an illegal value is passed. An enum defines values that can never change. You can never remove a value from an enum, and you can never create another instance of an enum. Also, when you have a variable whos type is an Enum, then the ONLY values that can be assigned to the variable are the values that have been defined in the enum. Furthermore, an enum can be used in a switch statement. When used in a switch statement, a runtime exception will occur if a null value is ever passed to the switch statement. That way you don't have to account for a situation were something passed a null value for the state (assuming a null value CAN be assigned to an enum variable. At the moment I cannot remember if a null value is allowable for enums). @ANY OTHER MEMBERS: feel free to address anything that I missed or to clarify anything if it is vague.

  9. #9
    JimmyD is offline Member
    Join Date
    Oct 2011
    Location
    New Jersey
    Posts
    44
    Rep Power
    0

    Default Re: How to update state variables in Objects (similar to pass by reference)

    Thanks Kenny! That's very thorough explanation!

  10. #10
    kennyman94 is offline Member
    Join Date
    Feb 2011
    Posts
    24
    Rep Power
    0

    Default Re: How to update state variables in Objects (similar to pass by reference)

    You are welcome.

Similar Threads

  1. How to save large amounts of similar objects?
    By nieuwenhuizen-jk in forum New To Java
    Replies: 12
    Last Post: 05-13-2011, 04:21 PM
  2. static variable /pass by reference
    By katturv in forum New To Java
    Replies: 15
    Last Post: 10-03-2010, 09:17 AM
  3. How to create pass by reference??
    By --> xeiyne! in forum CLDC and MIDP
    Replies: 4
    Last Post: 04-08-2010, 07:43 PM
  4. Don't pass by reference
    By Lyven in forum Advanced Java
    Replies: 6
    Last Post: 11-16-2009, 07:06 PM
  5. Pass-by-reference
    By popjava in forum New To Java
    Replies: 1
    Last Post: 10-19-2009, 04:45 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
  •