This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Accessing private variables in equals() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Accessing private variables in equals()" Watch "Accessing private variables in equals()" New topic
Author

Accessing private variables in equals()

Jagdish Shinde
Greenhorn

Joined: Nov 07, 2007
Posts: 9
Hello,
I am trying to compare two objects by overriding equals(Object o) method.
Code:
public class TestEquals{
private int a;
private int c;
private boolean val=false;
public boolean equals(Object o){

TestEquals te=null;
if(o instanceof TestEquals)
{
te = (TestEquals)o;
if((this.a==te.a)&&(this.c==te.c))
val=true;
else val= false;
}

return val;
}

How can I access private variables a&c using te.a and te.c??
I thought i need to write getters for this.
(And this is valid for any method (other than equals())in TestEquals) class )

Please help..

Thanks.
Harshit Rastogi
Ranch Hand

Joined: Apr 15, 2008
Posts: 131
Originally posted by Jagdish Shinde:
Hello,
How can I access private variables a&c using te.a and te.c??
I thought i need to write getters for this.
(And this is valid for any method (other than equals())in TestEquals) class )

Thanks.


just create getter for the variable a & c and use them to access private variable. Getter should be public method returning the variable.


<a href="http://technologiquepanorama.wordpress.com" target="_blank" rel="nofollow">My Techie Blog</a><br /><a href="http://www.java-questions.com" target="_blank" rel="nofollow">Java Questions</a>
Jagdish Shinde
Greenhorn

Joined: Nov 07, 2007
Posts: 9
if((this.a==te.a)&&(this.c==te.c))

I thought i wont be able to access private variables a & c using te.a and this line would cause compilation error. But it is compiling with this line.
My question is, since a & c are private I would be restricted from using '.' operator to access a,c and a public getter was needed. but it is compiling fine.
Is this some special case!?
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Jagdish Shinde:
if((this.a==te.a)&&(this.c==te.c))

I thought i wont be able to access private variables a & c using te.a and this line would cause compilation error. But it is compiling with this line.
My question is, since a & c are private I would be restricted from using '.' operator to access a,c and a public getter was needed. but it is compiling fine.
Is this some special case!?


No, it's not a special case. Access modifiers work on the class level, not on the instance level. So, private members can be accessed from anywhere inside the same class, no matter what object they belong to.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Jagdish Shinde
Greenhorn

Joined: Nov 07, 2007
Posts: 9
Thanks Ilja,
I got the point.

This probably worked because the reference 'te' used was of the same class TestEquality where I was accessing private variables.


Thanks...
 
 
subject: Accessing private variables in equals()
 
Similar Threads
Want hepl to understand Implementin an equals()
instanceof
Generics and collections K&B pg 526
why they use hashCode() method in equals() method
instance of problem