Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

accessing private members of an instantiated class from its main method  RSS feed

 
Arvind Rasengan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Please help me out with this basic question. I have a class with a standard main method. The class has private variables and functions. I am instantiating this class in the main method. Why does java allow me to refer/call private variables and functions of the instantiated object in the main method? I can understand if I am calling any private static methods or variables directly in main.

-Arvind
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mega Lodon wrote:

Please check your private messages for an important administrative matter
 
Devaka Cooray
Marshal
Posts: 5554
707
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can access them within the same class as they declared.
 
Arvind Rasengan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was hoping for a more detailed explanation but thanks.
 
Devaka Cooray
Marshal
Posts: 5554
707
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arvind Rasengan wrote:I was hoping for a more detailed explanation but thanks.

Have a look at this article. If your private attributes were declared in the same class as where the calling method (here, the main method) exists, you can access them because the 'caller' and the attributes are in the same class.

hth
 
Arvind Rasengan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for prompt reply. I am somewhat familiar with Java access rules. Let me elaborate using sample code below. Please take a look at the comments also. Thanks



 
Ryan Webb
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
because main method is from within the class jranchquestion1 so private members are still accessible.

At first I am also confused about it too...let me put it this way - main method is a very special method.

If for example you place your main method in other class (say class jranchquestion2) then you cannot access the private method.

hope that clears it.
Ryan Webb
Philippines
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16026
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The access rules are for the class and are not per object. An object of class A can access the private variables of any object of the same class A - not just the private variables of that object itself.

Arvind Rasengan wrote:// Java allows me to access private functions though...Not private variables!
// For e.g., I cannot refer myObj.pvtvar . Don't understand the different treatment.

That's not true; you can directly access the member variables, if you're doing it in the same class. Try it out...

Ryan Webb wrote:...let me put it this way - main method is a very special method.

No it isn't, main() is just a method like any other method, and there are no special rules with regard to access for the main() method. It works just like any other method.
 
Arvind Rasengan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jesper and Ryan for clarifying things for me!

That's not true; you can directly access the member variables, if you're doing it in the same class. Try it out...




You're right Jesper, I made a hasty test and jumped to the wrong conclusion earlier. Guess I will check out the jranch FAQ also.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!