The Artima Developer Community
Sponsored Link

Java Answers Forum
private variables

1 reply on 1 page. Most recent reply: Mar 1, 2003 2:26 PM by Sri Nivas

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Jonathan Hall

Posts: 1
Nickname: jonnyhall
Registered: Feb, 2003

private variables Posted: Feb 28, 2003 8:56 PM
Reply to this message Reply
Advertisement
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?

thanks,
Jonnyhall


Sri Nivas

Posts: 5
Nickname: seeknew
Registered: Mar, 2003

Re: private variables Posted: Mar 1, 2003 2:26 PM
Reply to this message Reply
the private variable iamprivate gets a default value of 0 since it is an int.

So the two variables can be compared.

Flat View: This topic has 1 reply on 1 page
Topic: Printing in java Previous Topic   Next Topic Topic: File I/O  questions

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use