File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with static method

 
ronnir paterl
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
amit punekar
Ranch Hand
Posts: 544
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Xyz Abc
Greenhorn
Posts: 27
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic