• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static context ><

 
Jonathon Stride
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well its giving a nullpointerexception then its a choke at runtime.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm talking about when the reference is null and the method is static.
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...

No, you can call static methods on null references: the following works under both 1.5 and 1.6:
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks all that makes sense now =)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic