File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Practice Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Practice Question" Watch "Practice Question" New topic

Practice Question

Praveen Mathur

Joined: Sep 21, 2003
Posts: 18
Hi Javaranchers:
Please try to predict the output of this question:
class A {
static A createA() {
return new A();
} // createA()
} // A

public class Tester {
public static void main(String[] parms) {
A aVar = null;
} // main()
} // Tester

"The self-controlled soul, who moves amongst sense objects, free from either attachment or repulsion, he wins eternal Peace." - Bhagavad Gita
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
It will print hashCode for an object of class A.
Ed B.

Joined: Jul 21, 2003
Posts: 9
I think it should throw a NullPointerException as the aVar is null. The createA method is static so it can be invoked like A.createA().
Ed B.

Joined: Jul 21, 2003
Posts: 9
Now that I tested the class, it does print out the hashcode. I am truly amazaed
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
Barkat's right: there's no problem with the code, you don't have to have an instance of a class to call its static(class) methods. e.g., Math class.
I agree. Here's the link:
subject: Practice Question
It's not a secret anymore!