• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

static methods and NULL

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Test {
public static Test aa() { return null; }
public static int bb() { return 5; }
public static void main(String[] args) {
System.out.print(aa().bb());
}
}
prints out "5".
Why there is no NullPointerException???
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Eugene,
I think that's because you do not need an instance of one class to access its static method. Since bb() method is a static one, you can have aa() method returning null value and still access bb() method without NullPointerException thrown.
Try removing the static modifier in bb() method.. you'll get NullPointerException thrown. It's because to access a non-static method, one needs to have an instance of the encapsulating class.
correct me if I'm wrong..
- eric
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
u do not get a NullPointerException because Test is an object which is null.
if u remove the static modifier in bb() u will get a compiler error complaining about accessing non-static methods from a static method.
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
Q1) what do we exactly mean when we say aa().bb()
Q2) what is the diff btwn calling by se.bb() and aa().bb() ?
Q3) what do you mean by "int cannot be dereferenced" .when i say aa().bb().cc() it gives me an error saying, int cannot be dereferenced.
same case when i say bb().cc()
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
Because NullPointException extends RuntimeException, NullPointException is thrown at Runtime.In other words, NullPointException is checked at Runtime.And aa() method is static method,it is loaded at compile-time.null has binding at compile-time.
So, NullPointException is not thrown.
[ July 20, 2002: Message edited by: Val Lee ]
[ July 20, 2002: Message edited by: Val Lee ]
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's another example of the fact that a null reference can be used to access a static member of a class.

In the above example, there really isn't a lot of difference between accessing the static member as Green.a or g3.a. Even though g3 has a null value, the type is still Green so the compiler knows exactly what class member is being accessed. Similarly, the following would even work.

When accessing a static member, the type of the reference is what is important. The actual value of the reference doesn't make any difference. Static references are resolved at compile time so the run time value of the reference makes no difference.
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following example from my mock exam (at the URL contained in my signature) illustrates the fact that a reference to a static member is resolved at compile time based on the type of the reference. In contrast, a reference to an instance member is resolved at run time. Of course, the reference in this case is the implicit "this" reference.
At compile time, the type of "this" is the same as the type of the class in which "this" appears. In the example below, that type is P. At runtime, however, the type of "this" is type Q because the method printS1S2 is invoked as a member of Q.
When you take a look at the following code, remember that the reference to the static method is resolved at compile time but the reference to the instance method is resolved at runtime.
I don't want to spoil the fun, so I won't post the correct answer unless someone asks for it.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic