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

Thread: Can an object change it's own reference?

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Can an object change it's own reference?

    Hi,

    I was wondering if you could do something like this in java:

    public Class Actor {

    ...
    Object Object 1 = new Object(...);
    ...

    public void function() {

    this.Object1 = this.Object1.loop();

    }

    }

    where

    public Class Object {

    ...

    public Object loop() {

    Object Object2 = new Object(...);
    ...
    return Object2;

    }

    }



    Could this cause reference problems? Is there any "cleaner" way to make an object spawn its own successor?

    Thank you in advance


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Can an object change it's own reference?

    The assignment statement:
    this.object1 = this.object1.loop();
    would change the value in the reference variable. There should be no problem assigning the variable a new value.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    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: Can an object change it's own reference?

    Object1 = Object1.methodCall();

    In other generic terms:

    VariableName = VariableName.returnValueFromMethodCall();

    Essentially this says set the value of the variable to the value returned by the method call. As long as the types match there should be no problem. It does not matter which object the method call belongs to, even the same object.
    Think about what happens to the object previously stored in the variable Object1 when the line of code executes. Where does it go?

  4. #4
    Junior Member
    Join Date
    Dec 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can an object change it's own reference?

    When this.object1.loop(); is called new object gets created.
    When above object is assigned to this.object1 the reference to old object is lost and will garbage collected.

Similar Threads

  1. [SOLVED] Help regarding Superclass variable can reference subclass object
    By rohan22 in forum Java Theory & Questions
    Replies: 8
    Last Post: July 12th, 2011, 01:30 AM
  2. Object Reference
    By Mr.777 in forum Object Oriented Programming
    Replies: 2
    Last Post: June 13th, 2011, 03:03 AM
  3. Passing reference via object
    By Stefan_Lam in forum Java Theory & Questions
    Replies: 1
    Last Post: January 7th, 2011, 11:57 AM
  4. 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
  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