• 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

accessing private members of an instantiated class from its main method

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mega Lodon wrote:


Please check your private messages for an important administrative matter
 
Sheriff
Posts: 7137
1360
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was hoping for a more detailed explanation but thanks.
 
Devaka Cooray
Sheriff
Posts: 7137
1360
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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



 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic