Home > Java > Self Test Java -1

Self Test Java -1

Yesterday, one of my colleague casually mentioned something while having lunch together. This is what he said “I am having a class in jar” On which I can do

Class.foName("fully qualified class name");

But, I am not able to do

clazz.newInstance();

The question stuck in my mind. But instead of going ususal root of opening a google box and tamly looking for answer I thought to self test me too. It should be easy pick and I got it bang on target except for the Exception names and exact jargon of the words. One should not be so hard on himself.

Quotuing from javadoc

Returns:
a newly allocated instance of the class represented by this object.
Throws:
IllegalAccessException - if the class or its nullary constructor is not accessible.
InstantiationException - if this Class represents an abstract class, an interface, an array class, a primitive type, or void; or if the class has no nullary constructor; or if the instantiation fails for some other reason.
ExceptionInInitializerError - if the initialization provoked by this method fails.
SecurityException - if there is no permission to create a new instance.

 

Now this gonna be my new questions in the interviews I take. M gonna have some fun.

Advertisement
Categories: Java Tags:
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.