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

Thread: scope of a class created as local variable

  1. #1
    Member
    Join Date
    Jul 2012
    Posts
    90
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default scope of a class created as local variable

    My question is.Will the temp variable will be set to null,so it is going to be de-allocated after the termination of the function?
    NothingHere is a class.voidPositionList is a list.
    private final void initVoidPositions() {
            NothingHere temp = new NothingHere("Road");
            voidPositionList.add(temp);
        }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,571
    Thanks
    49
    Thanked 2,232 Times in 2,204 Posts

    Default Re: scope of a class created as local variable

    The variable: temp will disappear when the method exits.
    The object that temp refers to has a reference saved in the list and will continue to exist and be accessible.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jul 2012
    Posts
    90
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: scope of a class created as local variable

    Thank you very much.If no reference exists to the object,then it is going to be removed by garbage collector.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,571
    Thanks
    49
    Thanked 2,232 Times in 2,204 Posts

    Default Re: scope of a class created as local variable

    Yes, that's how its supposed to work.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Jul 2012
    Posts
    90
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: scope of a class created as local variable

    Thank you very much for you laconic answers.

  6. #6
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    939
    Thanks
    6
    Thanked 202 Times in 178 Posts

    Default Re: scope of a class created as local variable

    Quote Originally Posted by Samaras View Post
    Thank you very much.If no reference exists to the object,then it is going to be removed by garbage collector.
    Norm (#4) is right. But I don't think any promise is made about when memory will be freed. Or whether it will be freed this side of your program's termination.

    The point is that a reference to the object does exist as one of the things in the list, so the newly created NothingHere instance is quite "safe".

  7. #7
    Member
    Join Date
    Jul 2012
    Posts
    90
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: scope of a class created as local variable

    Quote Originally Posted by pbrockway2 View Post
    Norm (#4) is right. But I don't think any promise is made about when memory will be freed. Or whether it will be freed this side of your program's termination.
    Yes,i agree with you too

Similar Threads

  1. scope problem: append to text field declared out of class
    By chopficaro in forum Java Theory & Questions
    Replies: 2
    Last Post: July 14th, 2012, 03:21 PM
  2. Using charAt and other string methods in a created class
    By briankfmn in forum Object Oriented Programming
    Replies: 1
    Last Post: February 8th, 2012, 12:54 AM
  3. Replies: 5
    Last Post: November 16th, 2011, 10:22 AM
  4. Variable Scope
    By TP-Oreilly in forum Java Theory & Questions
    Replies: 7
    Last Post: October 5th, 2011, 04:08 PM
  5. Running methods of a class that created you...
    By joestr in forum Java Theory & Questions
    Replies: 2
    Last Post: April 3rd, 2011, 01:59 PM