Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: how to maintain a unpopulated class -newbie level

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default how to maintain a unpopulated class -newbie level

    A little while back I wrote a class that passed an instance into a routine and looped several times. What I needed was for the instance to be as it was intially with some fields set and some reverting to how they were before the field values were set.
    When returning from the work class it would always have populated fields as expected. I did a rubbish hack and recreated
    the instance and initialised some of the fields that needed pre-initialising.
    I know this is probably a newbie question but how would i have saved the instance in the initialised state before the worker
    class populated the rest of the fields. If i tried to save it to another Object it would point to the same instance and all the
    changes were reflected in the cloned instance.
    Is this a case of using a clone method or using the New keyword.
    Sorry this is so dense and badly explained.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: how to maintain a unpopulated class -newbie level

    What happened when you tried cloning?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    May 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how to maintain a unpopulated class -newbie level

    It did not clone the required fields - would have to have written bespoke clone method which is the same effort as the hack I did.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: how to maintain a unpopulated class -newbie level

    You might consider posting an SSCCE that demonstrates why cloning didn't work.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: how to maintain a unpopulated class -newbie level

    Quote Originally Posted by wuwu_sister View Post
    If i tried to save it to another Object it would point to the same instance and all the changes were reflected in the cloned instance.
    Objects are passed by reference in Java. My guess is that when you "saved it to another Object" all you did was make a second variable point at the same existing Object, and then proceed to modify this object by the worker class. Show the code used to "save it to another Object" before the worker class does the modifications.

Similar Threads

  1. maintain value of integer
    By shachee in forum What's Wrong With My Code?
    Replies: 8
    Last Post: February 28th, 2013, 10:33 AM
  2. how to set up level class in 2d java game
    By hwoarang69 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 25th, 2012, 06:17 PM
  3. newbie question: Error: Could not find or load main class Java Result: 1
    By ideaman in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 1st, 2012, 11:40 PM
  4. Maintain focus on JFrame
    By nik_meback in forum AWT / Java Swing
    Replies: 1
    Last Post: December 15th, 2010, 08:49 AM
  5. Newbie questions about lesson on class implementation
    By Jonnybravo9 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 7th, 2010, 09:04 PM