Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

private member doubt

 
Wiyanto Ngasinur
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


does anyone know why private variable court can be used with reference s ? as i know private variable can be used only inside the class who is declared it, but not thru reference...
 
Srinivas Kumar
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It should give compilation error
 
Wiyanto Ngasinur
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think so too, but after i compile it. no compile time error ? what the heck is happening here ??
 
Sourin K. Sen
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Really Amazing. I too thought it would have given compilation error.


It really shouldnt have worked in any case but it is working.......
I would really like to know what's going on here.
How come the Java syntaxes are being violated this way???
[ March 23, 2007: Message edited by: Sourin K. Sen ]
 
Prasad Shindikar
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
private members are not accessible outside the class.

since the private member variable count, is being accessed inside the
class Sandys the variable count is accessible.

However if you try to instantiate an object of Sandys in some other class, then it is not possible to access the variable count.
 
Wiyanto Ngasinur
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


is above code not considered as accessing variable s outside of class, since it's accessing thru reference ?
 
Pratik Patel
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. Private members can be accessed from anywhere in the class. But it can not be accessed outside the class.

Sourin K. Sen, The rules are not violated here. You can not have direct access to non static members from the static method. But you can access it with the reference of an object, otherwise you will not be able to create an instance of a class from main method using class's constructor since it's also a non static block.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
otherwise you will not be able to create an instance of a class from main method using class's constructor since it's also a non static block

I'm not clear about that, the constructor being public
 
Pratik Patel
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By that line, I was talking about access to static and not static members as 'Sourin K. Sen' told that java is violating the rule(error 2 in his last post).

Private members can be accessed from anywhere in the class using the object reference. It can not be accessed outside the class.
 
Sourin K. Sen
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know i was wrong abt the 2nd point hence edited the post. It just struck me after i posted the reply & immediately went for lunch.

But anyways, still i'm not convinced. I think i need a better explanation.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think i need a better explanation.

So do I
s.court looks like accessing from outside.
 
Wiyanto Ngasinur
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Need some explanation too
 
Henry Wong
author
Marshal
Pie
Posts: 21003
77
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Wiyanto Ngasinur:
Need some explanation too


Yes, this does seems weird... but as already mentioned, private means private to the class, not private to the instance. This means that any method of the class can access any private member of the same class, regardless of whether it is the same instance or not.

Henry
 
Wiyanto Ngasinur
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
regardless of whether it is the same instance or not


hi Henry, thanks for the reply but i still not understand, maybe can you be more verbose ... why private variable court can be accessed thru reference ? since it considered as accessing from outside
 
Henry Wong
author
Marshal
Pie
Posts: 21003
77
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Wiyanto Ngasinur:


hi Henry, thanks for the reply but i still not understand, maybe can you be more verbose ... why private variable court can be accessed thru reference ? since it considered as accessing from outside



Private has nothing to do with whether it is accessed via a reference or not. It is perfectly fine to access a private member from a different object -- as long as this different object is of the same class type. In this case, it is not even being accessed from a different object. It is being access from a static method, of the same class type.

I don't know how can I be more verbose on this.

Henry
 
Wiyanto Ngasinur
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's enough verbose , Henry. At last, i understand. Thank you...
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Private has nothing to do with whether it is accessed via a reference or not. It is perfectly fine to access a private member from a different object -- as long as this different object is of the same class type. In this case, it is not even being accessed from a different object. It is being access from a static method, of the same class type.

I've just felt a rock falling on my head now
Why is this working like that ? Is this considered as being object-oriented ?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Private has nothing to do with whether it is accessed via a reference or not. It is perfectly fine to access a private member from a different object -- as long as this different object is of the same class type. In this case, it is not even being accessed from a different object. It is being access from a static method, of the same class type.

I've just felt a rock falling on my head now
Why is this working like that ? Is this considered as being object-oriented ?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Private has nothing to do with whether it is accessed via a reference or not. It is perfectly fine to access a private member from a different object -- as long as this different object is of the same class type. In this case, it is not even being accessed from a different object. It is being access from a static method, of the same class type.

I've just felt a rock falling on my head now
Why is this working like that ? Is this considered as being object-oriented ?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Private has nothing to do with whether it is accessed via a reference or not. It is perfectly fine to access a private member from a different object -- as long as this different object is of the same class type. In this case, it is not even being accessed from a different object. It is being access from a static method, of the same class type.

I've just felt a rock falling on my head now
Why is this working like that ? Is this considered as being object-oriented ?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Private has nothing to do with whether it is accessed via a reference or not. It is perfectly fine to access a private member from a different object -- as long as this different object is of the same class type. In this case, it is not even being accessed from a different object. It is being access from a static method, of the same class type.

I've just felt a rock falling on my head now
Why is this working like that ? Is this considered as being object-oriented ?
 
Sharan Ashok Vasandani
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think main is inside the sandys class thats why it is able to access the variable even though it is private.


i you move main in some other class its giving compiler error.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic