File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Author

Practice Question

Praveen Mathur
Greenhorn

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;
System.out.println(aVar.createA().hashCode());
} // 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.
Greenhorn

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().
Adnan
Ed B.
Greenhorn

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.
 
jQuery in Action, 2nd edition
 
subject: Practice Question
 
Similar Threads
Static
One of the Mock Questions Cannot be Compiled
Confusion with Constructor
why is it wrong?
Null Pointer Exception ???