Add to Google Reader or Homepage

How to resolve java.lang.instantiationexception

Instantiation Exception

I have discussed about this exception in detail in my new post.To have a look at it click here..

In this Post let us discuss about the java.lang.instantiation exception in detail.Let us know what is meant by this exception?why this exception is thrown? and how to solve it.

What is meant by Instantiation Exception?


There are certain classes exists for which we cannot create objects.Lets see what are those classes.

1.We all know that an abstract class cannot be instantiated.But there are some other ways available, through which we can try to instantiate a abstract class.If we use the newInstance() method for creating the object then we will not get that error at compile time.But you will get this Instantiation exception thrown.

2.An abstract class,an interface,even primitive data types like int,float,double,byte can also be represented as the Class objects.Except ordinary class other classes cannot be instantiated.

3.A class that has no default constructor i.e a constructor with no arguments.This default constructor is needed only when an argumented constructor is already defined, otherwise it is not needed.

Look at the following example to understand how to use newInstance() method

class A
{
}
class B
{
......
Class a=Class.forName("A");//representing an ordinary class
a.newInstance();
}

As you see this is another way of instantiating a class.The Class.forName() method will return the object of class Class.Using that object we can call the newInstance() method to instantiate the class A.

Similarly  for an interface like Serializable() we can do as follows

Class b=Serializable.class;
b.newInstance();

Here Serializable is an interface but i have represented it as a class using the newInstance() method.

When will be this exception is thrown?

 

When we try to instantiate the following classes using the newInstance() method, then this exception will be thrown.

1. Trying to instantiate an abstract class.

2.statements that involves instantiating an interface.

3.An array class

4.A legitimate class that has no nullary constructor.


Example situation of this exception:

instantiating an abstract class:


import java.io.*;
import java.lang.*;
import java.util.*;

abstract class Inst
{
}
class Exp
{
public static void main(String args[])throws InstantiationException,IllegalAccessException,ClassNotFoundException
{
Class cl=Class.forName("Inst");//trying to instantiate an abstract class "Inst"
cl.newInstance();
}
}
output:

Exception in thread "main" java.lang.InstantiationException
        at sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance
(Unknown Source)
        at java.lang.reflect.Constructor.newInstance(Unknown Source)
        at java.lang.Class.newInstance0(Unknown Source)
        at java.lang.Class.newInstance(Unknown Source)
        at Exp.main(Exp.java:15)

 

Representing Primitive data types as class:

 

import java.io.*;
import java.lang.*;
import java.util.*;

 class Inst
{
}
class Exp
{
public static void main(String args[])throws InstantiationException,IllegalAccessException,ClassNotFoundException
{
Class cl=int.class;//representing the integer data types as a class
cl.newInstance();
}
}

output:
Exception in thread "main" java.lang.InstantiationException: int
        at java.lang.Class.newInstance0(Unknown Source)
        at java.lang.Class.newInstance(Unknown Source)
        at Exp.main(Exp.java:13)

 

Representing an interface as a class:


import java.io.*;
import java.lang.*;
import java.util.*;

 class Inst
{
}
class Exp
{
public static void main(String args[])throws InstantiationException,IllegalAccessException,ClassNotFoundException
{
Class cl=Serializable.class;//Representing the serializable interface as a class
cl.newInstance();
}
}

Exception in thread "main" java.lang.InstantiationException: java.io.Serializabl
e
        at java.lang.Class.newInstance0(Unknown Source)
        at java.lang.Class.newInstance(Unknown Source)
        at Exp.main(Exp.java:13)

 

Having no nullary constructor:


import java.io.*;
import java.lang.*;
import java.util.*;

 class Inst
{
int first,second;
Inst(int a,int b)    //Here there is no default constructor i.e. a constructor with no arguments
{
first=a;
second=b;
}
}
class Exp
{
public static void main(String args[])throws InstantiationException,IllegalAccessException,ClassNotFoundException
{
Class cl=Inst.class;
cl.newInstance();
}
}

output:

Exception in thread "main" java.lang.InstantiationException: Inst
        at java.lang.Class.newInstance0(Unknown Source)
        at java.lang.Class.newInstance(Unknown Source)
        at Exp.main(Exp.java:19)

Solution:


In java, as far i know there is no way available to instantiate an abstract class or an interface or the primitive data types.So,its better to avoid these statements from the program.If this exception arises due to the absence of a default constructor,it can be overcome by  defining a no argument constructor(default constructor)




99 comments:

dhavamani shiva said...

My cousin recommended this blog and she was totally right keep up the fantastic work!










Java Training Courses

Musharraf Al-Sakran said...

Thanks for your help .

i fixed my problem via your solution :)

prathap kumar said...

Very clear explanation.


prathap
java training in chennai

Martina Christy said...

Hi, Thanks for sharing this valuable blog. I did HTML5 course at reputed HTML5 Training Institutes in Chennai. This is reslly useful for me to make a bright future in designing field.

robin singh said...

Thanks for sharing informative article on java application development. Your post helped to understand the career in Java. JAVA Training in Chennai

mathew delport said...

Thanks for sharing this information. SEO is one of the digital marketing techniques which is used to increase website traffic and organic search results. If anyone wants to get SEO Training in Chennai visit FITA Academy located at Chennai. Rated as No.1 SEO Training institute in Chennai.

dhanamlakshmi palu said...

Thanks for sharing this article. You have shared lot of useful tips about Java .It helped a lot to improve my skill.
html5 Training in chennai | html5 Training chennai | html5 course in chennai | html5 course chennai

Suranka VMware said...

Your posts is really helpful for me.Thanks for your wonderful post. I am very happy to read your post.
VMWare Training in chennai | VMWare Training chennai | VMWare course in chennai

Andrew Son said...

The information you have given here are most worthy for me. I have implemented in my training program as well, thanks for sharing.

Hadoop Training Chennai
Hadoop Training in Chennai

kovalan Jayamurugan said...

Thanks for sharing your informative article on Hive ODBC Driver. Your article is very descriptive and assists me to learn whole concept in detail. Hadoop Training in Chennai

Abhi said...

hi want to call parameterised constructor for newInstance() & invoke(). Can you pls tell me how to do that

Roshini RS said...

This information is impressive; I am inspired with your post writing style & how continuously you describe this topic. After reading your post, thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic.
Regards,
Informatica training center in Chennai|Informatica training chennai|Informatica course in Chennai

Pooja Doss said...

Oracle DBA Training in Chennai
Thanks for sharing this informative blog. I did Oracle DBA Certification in Greens Technology at Adyar. This is really useful for me to make a bright career..

Pooja Doss said...

Whatever we gathered information from the blogs, we should implement that in practically then only we can understand that exact thing clearly, but it’s no need to do it, because you have explained the concepts very well. It was crystal clear, keep sharing..
Websphere Training in Chennai

Pooja Doss said...

Data warehousing Training in Chennai
I am reading your post from the beginning, it was so interesting to read & I feel thanks to you for posting such a good blog, keep updates regularly..

Pooja Doss said...

Selenium Training in Chennai
Wonderful blog.. Thanks for sharing informative blog.. its very useful to me..

Pooja Doss said...

Oracle Training in chennai
Thanks for sharing such a great information..Its really nice and informative..

Pooja Doss said...

SAP Training in Chennai
This post is really nice and informative. The explanation given is really comprehensive and informative..

Pooja Doss said...

This information is impressive..I am inspired with your post writing style & how continuously you describe this topic. After reading your post,thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic..
Android Training In Chennai In Chennai

Pooja Doss said...

Pretty article! I found some useful information in your blog, it was awesome to read,thanks for sharing this great content to my vision, keep sharing..
Unix Training In Chennai

Pooja Doss said...

I found some useful information in your blog, it was awesome to read, thanks for sharing this great content to my vision, keep sharing..
SalesForce Training in Chennai

Pooja Doss said...

There are lots of information about latest technology and how to get trained in them, like Best Hadoop Training In Chennai in Chennai have spread around the web, but this is a unique one according to me. The strategy you have updated here will make me to get trained in future technologies Hadoop Training in Chennai By the way you are running a great blog. Thanks for sharing this blogs..

Alia Kumar said...

This is really an awesome article. Thank you for sharing this.It is worth reading for everyone. Visit us:
Oracle Training in Chennai

Alia Kumar said...

very nice blogs!!! i have to learning for lot of information for this sites...Sharing for wonderful information.Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing.
Oracle DBA Training in Chennai

Alia Kumar said...

great article!!!!!This is very importent information for us.I like all content and information.I have read it.You know more about this please visit again.
Oracle RAC Training in Chennai

Alia Kumar said...

Wonderful tips, very helpful well explained. Your post is definitely incredible. I will refer this to my friend.
SalesForce Training in Chennai

Alia Kumar said...

I am reading your post from the beginning, it was so interesting to read & I feel thanks to you for posting such a good blog, keep updates regularly.
Java Training in Chennai

Alia Kumar said...

Really awesome blog. Your blog is really useful for me. Thanks for sharing this informative blog. Keep update your blog.
PHP Training in Chennai

Alia Kumar said...

Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing.Nice article i was really impressed by seeing this article, it was very interesting and it is very useful for me..
Android Training in Chennai

Alia Kumar said...

Really awesome blog. Your blog is really useful for me. Thanks for sharing this informative blog. Keep update your blog.
SAP Training in Chennai

Alia Kumar said...

Excellent information with unique content and it is very useful to know about the information based on blogs.
Hadoop Training in Chennai

Nj Sn said...

This artcile helped me resolve my issue.. really very useful !!!

Amirtha rao said...


I have read your blog, it was good to read & I am getting some useful info's through your blog keep sharing... Informatica is an ETL tools helps to transform your old business leads into new vision. Learn Informatica training in chennai from corporate professionals with very good experience in informatica tool.
Regards,
Informatica training center in Chennai|Informatica training chennai

Diya Patel said...

This information is impressive; I am inspired with your post writing style & how continuously you describe this topic. After reading your post, thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic..
Informatica Training in chennai | QTP Training in Chennai



Harshita said...

Hello admin, thank you for your informative post on hadoop training in Chennai. It helped a lot in training my students during our hadoop training Chennai sessions. We at Fita, provide big data training in Chennai for students who are interested in choosing a career in big data.

Dinju Thomas said...

Oracle Training in chennai | Oracle D2K Training In chennai
This information is impressive; I am inspired with your post writing style & how continuously you describe this topic. After reading your post, thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic..

Dinju Thomas said...

Thanks for Information Oracle Apps Technical is a collection of a bunch of collected applications like accounts payables, purchasing, inventory, accounts receivables, human resources, order management, general ledger and fixed assets, etc which have its own functionality for serving the business
Oracle Apps Training In Chennai

Dinju Thomas said...

This information is impressive; I am inspired with your post writing style & how continuously you describe this topic. After reading your post, thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic..
Selenium Training in Chennai | QTP Training in Chennai

Melisa said...

Thanks for sharing the pretty post, if you focus the certification training to enhance your skill for attaining good job in IT industry, for that you have to take the valuable certification training of SAS for your career growth.
Regards,
sas training in Chennai|sas course in Chennai|sas training institute in Chennai

varshini devi said...

This technical post helps me to improve my skills set, thanks for this wonder article I expect your upcoming blog, so keep sharing...
Regards,
ccna course in Chennai|ccna training in Chennai|ccna courses in Chennai

Rohit P said...

That is right solution for java lang , this information is very useful, thanks to person who every posted this information and if any one has issue with regards to seo or if required training on seo we serve best on it seo training in bangalore

Naviya Nair said...

Great and Useful Article.

Online Java Course

Java Online Training

Java Course Online

J2EE training

online J2EE training

Best Recommended books for Spring framework

Java Interview Questions








Java Training Institutes in Chennai

Java Training in Chennai

J2EE Training in Chennai

java j2ee training institutes in chennai

Java Course in Chennai

Diya Patel said...

Best Java Training Institute In ChennaiThis information is impressive; I am inspired with your post writing style & how continuously you describe this topic. After reading your post, thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic..

Gopi Perumal said...

Really awesome blog. Your blog is really useful for me. Thanks for sharing this informative blog. Keep update your blog.

Regards,
CCNA Training in Chennai | CCNA Training Institute in Chennai | Best CCNA Training in Chennai

Jeanne Davies said...

One of the website templates available on the internet.local marketing seo going to share it with my friends so that they can get an idea of latest website designs

Regina Hilary said...

The war between humans, orcs and elves continues earn to die . Lead your race through a series of epic battles, using your crossbow to fend off foes and sending out units to destroy castleshappy wheels . Researching and upgrading wisely will be crucial to your success! There are 5 ages total and each one will bring you new units to train to fight in the war for you cause.
earn to die 2
Whatever you do, don’t neglect your home base because you cannot repair it and once it is destroyed, you lose! Age of War is the first game of the series and really sets the tone for the Age of War games . Also try out the Age of Defense series as it is pretty similar.
In this game, you start at the cavern men’s age, then evolvetank trouble ! There is a total of 5 ages, each with its units and turrets. Take control of 16 different units and 15 different turrets to defend your base and destroy your enemy.
The goal of the game also differs depending on the level. In most levels the goal is to reach a finish line or to collect tokens. Many levels feature alternate or nonexistent goals for the player.

Divit said...

Thank you for your information.


Hadoop Certification in Chennai

Nguyễn Quỳnh said...

Good read. Thanks for sharing it!
Máy lạnh Daikin

Iqbal Dlk said...

very useful information

2016 be projects in chennai

2016 ieee java projects in chennai

lenovo thinkpad service center chennai

Iqbal Dlk said...

very nice information
be projects in chennai
2016 ieee projects in chennai
lenovo ideapad service center chennai

kiran kumar said...

good post very nice best java training in chennai

UMA MAHESWARI said...
This comment has been removed by the author.
Mythily Muthu said...

The best db2 training in chennai.visit for» The best Db2 training in chennai

Mythily Muthu said...

Really awesome blog.100% job j2ee training in chennai.visit:» The best J2ee training in chennai

Mythily Muthu said...

100%job orienent obiee training in chennai.visit:» The best obiee training in chennai

Mythily Muthu said...

100%job orienent obiee training in chennai.visit:» The best obiee training in chennai

UMA MAHESWARI said...

100%job orienent obiee training in chennai. the best java training institute

UMA MAHESWARI said...

very useful information.
the best c++ training institute

Aswin Karthick said...

good one java training in chennnai

حسام داود said...

شركة تسليك مجارى بالرياض
level تسليك مجاري بالرياض
افضل شركة تنظيف بالرياض
تنظيف شقق بالرياض
شركة تنظيف منازل بالرياض
شركة غسيل خزنات بالرياض
افضل شركة مكافحة حشرات بالرياض
رش مبيدات بالرياض
شركة تخزين عفش بالرياض
شركة تنظيف مجالس بالرياض
تنظيف فلل بالرياض
ابغى شركة تنظيف بالرياض

mythily mythu said...

good post very nice. the best java training in chennai»
the best java training in chennai.

mythily mythu said...

good post very nice. the best java training in chennai»
the best java training in chennai.

Ajay Kumar said...

nice post for Selenium training

Iqbal Dlk said...

Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing

dot net training in chennai

php training in chennai

java training in chennai

UMA MAHESWARI said...

good post very nice..
DOT NET training in chennai

UMA MAHESWARI said...
This comment has been removed by the author.
mythily mythu said...

Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing.
tibco training in chennai

mythily mythu said...

good post very nice.
Microstrategy training in chennai

UMA MAHESWARI said...

• Great information, I like this kind of blog information really very nice and more I can easily new skills are develop after reading that post.
microstrategy training in chennai

UMA MAHESWARI said...

• Nice information in the post....Keep on sharing..
oracle training in chennai

yasar said...

ece projects in chennai

embedded projects chennai

vlsi projects chennai
embedded training in chennai
matlab training in chennai

Iqbal Dlk said...

Thanks for sharing this valuable information to our vision

hotels near us consulate chennai
hotels near uk embassy chennai
hotels near apollo hospital chennai
hotels near german consulate chennai

nura said...

Thanks for sharing
Android training in chennai

Iqbal Dlk said...

very nice and informative blog
java training in chennai
dot net training in chennai

Saradha Devi said...

informative post

Online Selenium Training | Selenium Training Institute in Chennai | Best Selenium Training Center in chennai | Best Automation Testing Training in Chennai

UMA MAHESWARI said...
This comment has been removed by the author.
Geetha Devi said...

Nice Blog to read
Android Training in Chennai | Best Android Training in Chennai | Best Android Training in Chennai with Placement

1croreprojects said...

Thanks for sharing this valuable information.
java projects in chennai
dotnet projects in chennai
mba projects in chennai

Saradha Devi said...

great blog thanks for sharing

Selenium Training | Selenium Training Institute in Chennai | Best Selenium Training Institutes in Chennai | Software Testing Training in Chennai

Justin ricardo said...

I am happy to learn how we can resolve java.lang.instantionexeception since it has been a major problem to many people. Thank you for discussing Instantiation Exception in details. Do you need SOP Writers to assist you in writing your application documents? We are there to assist you at anytime.

Xplorant Seo said...

Wonderful blog.. Thanks for sharing informative blog.. its very useful to me..

iOS Training in Chennai

yasar said...

very nice and informative blog
big data projects chennai
mobile computing projects chennai
cloud computing projects chennai
secure computing projects chennai

IKIA CONSULTING said...

Nice Post...


Real Estate Companies in Chennai

SYSADMIN ACADEMY said...

I am learning java, here i found lots useful information.

SYSADMIN ACADEMY said...

Best Linux Trainng Center in Chennai
Red Hat Linux Trainng in Chennai

Rekha J said...

This is my first visit to your blog, your post made productive reading, thank you. dot net training in chennai

jazz said...

I just see the post i am so happy to the communication science post of information's.So I have really enjoyed and reading your blogs for these posts.Any way I’ll be replay for your great thinks and I hope you post again soon...
Software Testing Training in Chennai

juanjuergens said...

I am definitely enjoying your website. You definitely have some great insight and great stories.
ccna training london

sunitha vishnu said...


This is excellent information. It is amazing and wonderful to visit your site.Thanks for sharing this information&its very useful to me...
Android training in chennai
Ios training in chennai

Aptron said...

Nice post!! Thanks for sharing this information and keep updating us.

Oracle BI Obiee Training Institute in Delhi
MySQL Training in Delhi
Oracle Apps Training Institute in Delhi

john stany said...


I wondered upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I’ll be subscribing to your feed and I hope you post again soon.

Android App Development Company

eva jasmine said...

Useful Information regarding handling the java errors
Java Training Courses

Priya Kannan said...

Inspiring writings and I greatly admired what you have to say , I hope you continue to provide new ideas for us all and greetings success always for you..Keep update more information..
Dot Net Training in Chennai

nutana meka said...

It's very useful information Mulesoft online training

Pankaj Singh said...

Thanks for the sharing this post.
SAP Certification Course in Gurgaon

john stany said...

This article is very much helpful and i hope this will be an useful information for the needed one.Keep on updating these kinds of

informative things...
iOS App Development Company
iOS App Development Company

Sharon Sandy said...

great and nice blog thanks sharing..I just want to say that all the information you have given here is awesome...Thank you very much for this one.
web design Company
web development Company
web design Company in chennai
web development Company in chennai
web design Company in India
web development Company in India

louis philip said...

Nice it seems to be good post... It will get readers engagement on the article since readers engagement plays an vital role in every blog.. i am expecting more updated posts from your hands.
Fitness SMS
Fitness Text
Salon SMS
Salon Text
Investor Relation SMS
Investor Relation Text

isabella jacob said...

it is really amazing...thanks for sharing....provide more useful information...
Mobile app development company

Post a Comment

 
java errors and exceptions © 2010 | Designed by Chica Blogger | Back to top