File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Problem with static method 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 "Problem with static method" Watch "Problem with static method" New topic
Author

Problem with static method

ronnir paterl
Greenhorn

Joined: Oct 18, 2007
Posts: 11
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
amit punekar
Ranch Hand

Joined: May 14, 2004
Posts: 507
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


Regards,
Amit
Xyz Abc
Greenhorn

Joined: Aug 24, 2007
Posts: 27
Amit is right, you need to instantiate test by using new.


[ December 01, 2007: Message edited by: Xyz Abc ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with static method
 
Similar Threads
static methods..
sample Q
static method query [KB book (Topic 4.10).]
static members
A quote from Khalid A. Mughal