Get your CodeRanch badge!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Target Reference and Static Methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Target Reference and Static Methods" Watch "Target Reference and Static Methods" New topic
Author

Target Reference and Static Methods

Garrett Smith
Ranch Hand

Joined: Jun 27, 2002
Posts: 401


comp.lang.javascript FAQ: http://jibbering.com/faq/
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
hi Garret,
well, if a method is static then it is invoked via the class rather than an instance. so actually the java runtime doesn't even use the object reference you return (as null) to invoke the method mountain().
u can try this as well,
Test t = null;
t.mountain();
it will work.
here is the code i wrote,

but when we compile this class and .class file is generated compiler takes care of the static method calls as a part of the compile optimization/simplification(if u would say)...
here is the .class file code after decompiling it via "cavaj" (one of the decompiler)
i have put only the main() method of the TestStaticRef class here..

here as u see favorite() and mountain() methods for TestStaticRef and TestStaticRef1 are called via the class name rather than the instance that u return at run time...
so it works!!
regards
maulin.
Garrett Smith
Ranch Hand

Joined: Jun 27, 2002
Posts: 401
Interesting. Thank you.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Target Reference and Static Methods
 
Similar Threads
NullPointerException
Confusing output
Null Pointer Exception ???
jls 15.10.1