Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

A "catch" with static method calls....

807606Feb 23 2007 — edited Feb 23 2007
I am quite puzzled by the following.
There is class which which creates an instance of a class from a 3d party library, as follows:

import com.3dparty.enterprise.notify.MessageListenerService;

public class StaticMethodInvocationTest
{
private static final String SOME_STRING = "someString";

public StaticMethodInvocationTest()
throws Exception
{
// try
// {
MessageListenerService service = new MessageListenerService(40000);
// }
// catch (Exception e)
// {
// e.printStackTrace();
// }
}

static String getSomeString()
{
return SOME_STRING;
}

public static void main(String[] args)
{
System.out.println( StaticMethodInvocationTest.getSomeString());
}
}

Note the static method getSomeString(). All it does is return a constant. As one would expect, once the code is compiled, this method can be called with or without the presence of the 3d party library, because it does not make any references to the 3d-party classes. And in fact, it works without the 3d party jar just fine.

Now: note the try/catch block that's commented out. If I un-comment that block and compile the code, then it will still run fine when the library jar is in the classpath, but fail with a "ClassNotFoundException", if the library is removed from the classpath.

Why? The only thing that changed in this example is that now we have a "catch" for any exception that COULD be thrown from the "new", but when we call the static method, we do not create any instances...of anything!

I ran this example on Sun JDK 1.4 and JRockit 1.5_6 with same results.

Unfortunately, for me this is more than just a curiosity. Understanding what's going on here may help me avoid some very annoying problem, so I'd appreciate any insights. Thanks!




- Alex

Message was edited by:
alex_yershov
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 23 2007
Added on Feb 23 2007
3 comments
349 views