6 Replies Latest reply on Apr 23, 2008 2:42 PM by 807601

    Object Reference Variables

    807601
      Hi there.
      My SCJP asks a question:

      True or false:
      Some reference variables live on the stack and some live on the heap.

      I know that Objects and their instance variables live on the heap.
      I would have thought true for the reason that a reference variable could be declared as an instance variable (On the heap) or a local variable (On the stack). The book says the above statement is true, but for the following reason:
      when an object has a reference variable, the reference variable lives inside the object, on the heap.
      I don't entirely understand this explanation, it has me confused.
      Any help appreciated.
      Thanks
        • 1. Re: Object Reference Variables
          807601
          My SCJP asks a question:
          It's a violation of the non-disclosure agreement to publicize SCJP questions.
          I don't entirely understand this explanation, it has me confused.
          Local variables (primitive and reference) live on the stack. Class and instance variables (primitive and reference) live on the heap.

          ~
          • 2. Re: Object Reference Variables
            807601
            Sorry, meant to say my SCJP book.
            And for some reason I cant edit the thread now.
            Thanks for the reply, but it still doesn't clarify the explanation in question.
            Im wondering how a reference variable can live inside an object on the heap???
            • 3. Re: Object Reference Variables
              3004
              shamrock08 wrote:
              Im wondering how a reference variable can live inside an object on the heap???
              Objects live on the heap. What is "inside" an object? What causes an object to take up memory? Its member variables.
              class Foo {
                int i = 1;
                String str = "abc";
              }
              
              ...
              
              class Bar {
                void baz() {
                  Foo foo = new Foo();
                }
              }
              The referfence variable foo is a local variable, so it lives on the stack. It points to a Foo object. That object is on the heap. Inside that object is an int member variable, i, and a String member variable, str. Since those variables are inside the object, which is on the heap, they're on the heap too.
              • 4. Re: Object Reference Variables
                3004
                shamrock08 wrote:
                I would have thought true for the reason that a reference variable could be declared as an instance variable (On the heap) or a local variable (On the stack).
                Yes.
                The book says the above statement is true, but for the following reason:
                when an object has a reference variable, the reference variable lives inside the object, on the heap.
                That's not any different than what you're saying.
                • 5. Re: Object Reference Variables
                  807601
                  shamrock08 wrote:
                  True or false:
                  Some reference variables live on the stack and some live on the heap.

                  ... The book says the above statement is true, but for the following reason:

                  when an object has a reference variable, the reference variable lives inside the object, on the heap.
                  Is that the entire justification? It manages not to mention or allude to the stack!
                  • 6. Re: Object Reference Variables
                    807601
                    Thanks for that,
                    rgds