wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes static methods and NULL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "static methods and NULL" Watch "static methods and NULL" New topic
Author

static methods and NULL

Eugene Karpenko
Greenhorn

Joined: Jul 16, 2001
Posts: 3
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???
Eric Pramono
Ranch Hand

Joined: Jul 09, 2001
Posts: 74
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
Namrata Shetty
Ranch Hand

Joined: Jul 18, 2001
Posts: 30
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.
srinivas bolloju
Ranch Hand

Joined: Jan 23, 2001
Posts: 112
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()


please use the [code][/code] tags when showing code. visit <a href="http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=ubb_code_page" target="_blank" rel="nofollow">http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=ubb_code_page</a> ,for more details
Val Lee
Ranch Hand

Joined: Nov 27, 2001
Posts: 41
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 ]

Val SCJP2 1.2<br />====>SCJD
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
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<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
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.
 
Don't get me started about those stupid light bulbs.
 
subject: static methods and NULL