private instance variables


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: private instance variables

  1. #1
    Join Date
    Mar 2003
    Posts
    14

    private instance variables

    I am a complete Java newby could someone look at the following passage?

    "New Java programmers might ask if one Alpha object can access the private members of another Alpha object. This is illustrated by the following example. Suppose the Alpha class contained an instance method that compared the current Alpha object (this) to another object based on their iamprivate variables:
    class Alpha {
    private int iamprivate;
    boolean isEqualTo(Alpha anotherAlpha) {
    if (this.iamprivate == anotherAlpha.iamprivate)
    return true;
    else
    return false;
    }
    }

    This is perfectly legal. Objects of the same type have access to one another's private members. This is because access restrictions apply at the class or type level (all instances of a class) rather than at the object level (this particular instance of a class). "

    What I don't understand about this passage is how the iamprivate variable got a value at all. Since no value was given to it in the Alpha class shown, how can there be a comparison between "this.iamprivate" and "anotherAlpha.iamprivate variable? The iamprivate variable is declared private, so no other class can give it a value, right? Can the Alpha class exist in more than one place in the program? Or, can a public method that was defined in the Alpha class be called in another class to access the private iamprivate variable?

    thanks,
    Jonnyhall

  2. #2
    Join Date
    Dec 2002
    Posts
    83
    It's important to understand the difference between Class and Object. An Object is an instance of a Class. The code in the example is all the Class. When it is in isEqualTo(Alpha anotherAlpha) it is assuming that someone else has created an Object of Alpha and is now dealing with it.

    Hmm hard to make this clear. Somewhere else someone has already created some Alpha objects and set the iamprivate value. Then they make the call to isEqualTo, so at that point the iamprivate fields DO have values and can be compared.

    This example illustration code might help. It creates two Alpha objects, sets the iamprivate fields (assume the Alpha class has setIamPrivate(int) method), and then calls isEqualTo(Alpha).
    Code:
    Alpha firstAlpha = new Alpha();
    Alpha secondAlpha = new Alpha();
    
    firstAlpha.setIamPrivate(10);
    secondAlpha.setIamPrivate(20);
    
    boolean isEqual = firstAlpha.isEqualTo(secondAlpha);
    So at the point where isEqualTo() is called, the Alpha objects have values of 10 and 20 for iamprivate.

    It can be confusing to the mind when you mix up what a Class and Object are. Hope this helped illustrate it in the context of your example.
    -- Steven

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center