aspose file tools*
The moose likes Beginning Java and the fly likes static context >< Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "static context ><" Watch "static context ><" New topic
Author

static context ><

Jonathon Stride
Ranch Hand

Joined: Aug 06, 2009
Posts: 34
in the following program :



gives me :

Exception in thread "main" java.lang.NullPointerException
at Apple.main(Apple.java:11)

and if i change
System.out.println(a.countGold(myGold,6));
to
System.out.println(Fruit.countGold(myGold,6));

i get :

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Cannot make a static reference to the non-static method countGold(int, int) from the type Fruit

at Apple.main(Apple.java:11)




However on changing the method countGold to static i get the output properly (13) ( in both the cases ie, fruit and a )

why is this happening ?

2 interviews failed cause of not having SCJP ( and counting...)
not anymore !

SCJP 6 (70%)

now ready to count other reasons :P...
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Please UseCodeTags. Unformatted code/config/etc. is difficult to read. You can edit your post to include them using the button or re-post the question with proper formatting.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Because you can access static methods using either the class name or an instance?

Or are you asking why you got an NPE? Because there wasn't an instance. Or are you asking why trying to access is statically failed before you made it static? Because it wasn't static.
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537



Does this carry any meaning..... there is no object, how can there be a method in it. Static methods are shared by all instances. Also as static methods are class methods they can be called by using the class name.


[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

If it's static, it *does* "carry meaning"; static methods can be called on null references.

It's unclear and *shouldn't* be done, but it works.
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

well its giving a nullpointerexception then its a choke at runtime.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I'm talking about when the reference is null and the method is static.
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

Compiler does allow it because it doesn't know anything about the object. All it checks is the reference type so it allows it. But as there is no object , its an exception. If this is what you meant.......a=null;a.nonstaticmethod() is also possible and allowed at compile..
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

...

No, you can call static methods on null references: the following works under both 1.5 and 1.6:
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

Yeah checked it....i never came across this type.....it does work... and one more thing i din't read the question properly. Original code the method is not static and thus the null reference won't work. But yes thanks David..... Everthing we learn something new. Java is vast. Even if we know complex things, some simple things always pass by.
Jonathon Stride
Ranch Hand

Joined: Aug 06, 2009
Posts: 34
thanks all that makes sense now =)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static context ><
 
Similar Threads
A spurious code ???
Declaration and Access Control
Declaration and Access Control
Help on this SCJP question
why can't a function be called from a static context