Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Beginning Java
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Beginning Java
Can objects of sam class can access each others private variables?if yes then how ?
Waez Ali
Ranch Hand
Posts: 43
posted 19 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I got this question when i was playing that roundup game
It is (#80)th question & it says yes it is possible.
please somebody make me understand this concept with some example i m new to javaranch....
Steven Bell
Ranch Hand
Posts: 1071
posted 19 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Yes, and a common example would be the equals method.
public class Foo{ private int someVal; public Foo(){ someVal = 5; } public void setVal(int otherVal){ someVal = otherVal; } //important method for your question here //standard equals method, I'll point out the relavent parts public boolean equals(Object o){ if(o == null){ return false; } if(!(o instanceof Foo)){ return false; } //at this point we know we have another Foo object Foo other = (Foo)o; //cast o to Foo object //now we will compare the private variables if(this.someVal == other.someVal){ return true; } return false; } }
This is a common example. Because we are in the same class, even though it is different instances, we can access the private fields.
Live ordinary life in an extraordinary way. Details embedded in this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
TagLibs --> can id have rtexprvalue=true ?
Interface provided by Sun
Can We extend a java class or interface in a JSP page
StringBuffer and String Confused!!
Session Migration Pg 269 HFSJ
More...