• 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

Problem with static method

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I came across this line in Khalid Mughal's book and I don't understand it.

"However, note that a static method in a class can always use a reference of the class's type to access its members, regardless of whether these members are static or not."

Please consider the foll. code.

class Test {
int no;

public static void main(String args[]) {
Test a;
a.no = 10;
}
}


Shouldn't a.no=10 work as I'm using reference of class type (a) to access its non static member (no).

Can someone please clarify this for me...

Thanks
 
Ranch Hand
Posts: 544
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
You need to instantiate Test i.e

and then use this reference to access any non static member variable of the class.

The note you mentioned said "reference of class's type to access its members ...". You should read it as "Initialized reference of class's type if that is what is causing confusion to you.
Hope that answers your question.
Regards,
amit
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Amit is right, you need to instantiate test by using new.


[ December 01, 2007: Message edited by: Xyz Abc ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic