aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes private member doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "private member doubt" Watch "private member doubt" New topic
Author

private member doubt

Wiyanto Ngasinur
Ranch Hand

Joined: Dec 11, 2006
Posts: 34


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

Joined: Jul 14, 2005
Posts: 52
It should give compilation error
Wiyanto Ngasinur
Ranch Hand

Joined: Dec 11, 2006
Posts: 34
i think so too, but after i compile it. no compile time error ? what the heck is happening here ??
Sourin K. Sen
Ranch Hand

Joined: May 02, 2006
Posts: 86
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 ]

Regards,<br />Sourin.
Prasad Shindikar
Ranch Hand

Joined: Feb 18, 2007
Posts: 114
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

Joined: Dec 11, 2006
Posts: 34


is above code not considered as accessing variable s outside of class, since it's accessing thru reference ?
Pratik Patel
Greenhorn

Joined: Mar 20, 2007
Posts: 9
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

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


[My Blog]
All roads lead to JavaRanch
Pratik Patel
Greenhorn

Joined: Mar 20, 2007
Posts: 9
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

Joined: May 02, 2006
Posts: 86
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

Joined: Nov 24, 2005
Posts: 14687
    
  16

I think i need a better explanation.

So do I
s.court looks like accessing from outside.
Wiyanto Ngasinur
Ranch Hand

Joined: Dec 11, 2006
Posts: 34
Need some explanation too
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18836
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Wiyanto Ngasinur
Ranch Hand

Joined: Dec 11, 2006
Posts: 34
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
Sheriff

Joined: Sep 28, 2004
Posts: 18836
    
  40

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

Joined: Dec 11, 2006
Posts: 34
That's enough verbose , Henry. At last, i understand. Thank you...
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

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

Joined: Nov 24, 2005
Posts: 14687
    
  16

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

Joined: Nov 24, 2005
Posts: 14687
    
  16

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

Joined: Nov 24, 2005
Posts: 14687
    
  16

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

Joined: Nov 24, 2005
Posts: 14687
    
  16

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

Joined: Feb 22, 2007
Posts: 12
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: private member doubt