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 3 of 3

Thread: Object Reference

  1. #1
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Object Reference

    Greetings everyone!!!
    I am actually confused about a concept. So, i want you guys to actually help me out. Forexample,
    class_A obj1=new class_A();
    Suppose the above statement initializes all instances of class_A with 5, 6 and 7.
    Now,
    class_A obj2=new class_A();
    obj2=obj1;
    This is just a reference to the obj1. So, what if i want to assign all values of obj1 to obj2 but not as a reference, what should i do, other than using copy constructor.
    I am waiting for your suggestions.

    Regards...


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Object Reference

    Unfortunately, for objects Java only allows using the = operator as assigning object references.

    You can either manually re-assign the values, or use a helper method to copy the values for you.

    If keeping the specific object originally referenced by obj2 is important, I would recommend not using a copy constructor or clone method as these typically imply that you get a new object which happen to have the same values as the original.

    In this case, your code might look something like this:

    obj2.copyFrom(obj1); // copyFrom copies all values from obj1 into obj2

  3. #3
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Object Reference

    Thanks for the reply.

Similar Threads

  1. Reading from ResultSet to Object and from object Object Array
    By anmaston in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 7th, 2011, 06:11 AM
  2. Passing reference via object
    By Stefan_Lam in forum Java Theory & Questions
    Replies: 1
    Last Post: January 7th, 2011, 11:57 AM
  3. Object as a Reference into Object's Class
    By Ace Coder in forum Object Oriented Programming
    Replies: 6
    Last Post: November 30th, 2010, 12:22 PM
  4. cannot find symbol in reference variable
    By NPotter86 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 1st, 2010, 08:21 PM
  5. Object as Reference not working
    By jassi in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 9th, 2010, 09:47 AM