• 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

private member variable accessble?????

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi guys,

the following question is 45 th one in http://www.jchq.net/mockexams/exam3.htm

i have adoubt in the following program
==================================
public class test{
private int court;
public static void main(String argv[]){

test s = new test(199);
System.out.println(s.court);
}
test(int ballcount){
court=ballcount;
}
}
=====================================
if i compile the above program it prints :199


court is a private variable,any instance can not access a private variable directly like above .......


I compiled and run the program
I got the outout :199
I just wondered.......




Thanks in advance,
Sridhar Veesam
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, the private variables in a class definition can ONLY be accessed by an instance of the class (except through reflection possibly)

Since the main method is part of the class definition, you can access the private variables of the instance in the main method.
[ March 04, 2006: Message edited by: Keith Lynn ]
 
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi veesam,

Keith is correct. Main() is declared within the same class that contains the private member, so, main() also knows about it.
 
veesam sridhar
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank u guys
 
See where your hand is? Not there. It's next to this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic