Results 1 to 3 of 3

Thread: Refrences?

  1. #1

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Refrences?

    Can anyone please explain this to me. Im trying to understand the diffrence between invoking instanace methods on an object and accessing members of an object. I thought member access using a refrence was determined by the type of refrence not the class of the current object denoted by the refrence.

    But when i run this the output is 10 then 5, but if this was the case then the output should be 10 then 10 since Test is now denoted by the t1 refrence.

    Code:
    class Test{
        public int i = 5;    
          
      }
    
      class Test1 extends Test{
        public int i = 10;      
      }
    
     public class Test2 extends Test1{
        public static void main(String[] args){
             
        Test1 t1 = new Test1();  
         System.out.println(t1.i); // 10 
           
        Test t = t1; // member access using a refrence is determined by 
             // the type of refrence not the class of the current object 
     // denoted by the refrence.  
      
        
       System.out.println(t.i);  // 5

  2. #2
    VirtuallyVB
    Guest

    Thumbs up

    I use the term "member" for something being a part of the class. So I would say "member variable" versus "member method" for a data value versus a function. Also, I would say "instance member..." versus "class member...".

    RULE: In my terminology, "member variables" are accessed based on the reference and "member methods" are accessed based upon the object.

    The way you wrote your question, I'd think that you would be in agreement with the results 10, 5.

    Test t = t1; might be considered the same as
    Test t = new Test1(); if the line
    Test1 t1 = new Test1(); was omitted (and I only say that so that one object exists).

    In fact, that is a good point to make and I got that question on the cert exam. After the dust clears, how many references do you have and how many objects do you have?
    t and t1 are 2 total references and "new Test1()" is 1 total object.

    Now apply the rule. Which reference is being used to access the (member) variable? Then you will know which value to expect.

  3. #3
    VirtuallyVB
    Guest

    Thumbs up

    I use the term "member" for something being a part of the class. So I would say "member variable" versus "member method" for a data value versus a function. Also, I would say "instance member..." versus "class member...".

    RULE: In my terminology, "member variables" are accessed based on the reference and "member methods" are accessed based upon the object.

    The way you wrote your question, I'd think that you would be in agreement with the results 10, 5.

    Test t = t1; might be considered the same as
    Test t = new Test1(); if the line
    Test1 t1 = new Test1(); was omitted (and I only say that so that one object exists).

    In fact, that is a good point to make and I got that question on the cert exam. After the dust clears, how many references do you have and how many objects do you have?
    t and t1 are 2 total references and "new Test1()" is 1 total object.

    Now apply the rule. Which reference is being used to access the (member) variable? Then you will know which value to expect.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width