• Post Reply Bookmark Topic Watch Topic
  • New Topic

Dynamic class loading  RSS feed

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



out put
java.lang.InstantiationException: org.sri.corejava.basic.Jeep
at java.lang.J9VMInternals.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1325)
at org.sri.corejava.basic.ClassLoadingExample.main(ClassLoadingExample.java:8)

I tried with out constructor.It's working good.
Can you tell me how to use constructors in dynamic class loading ?
 
Ranch Hand
Posts: 445
1
Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now, one reason for this could be that a no-argument constructor has not been provided. Since you have declared/defined a constructor with arguments the default constructor provided by the JVM would be absent.

What happens when you provide a no-argument constructor?

Cheers,
Raj.
 
srinivas srinivasmeenavalli
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
working with no-arguments constructor .
 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you instantiate a class with newInstance(), the class you are instantiating must have a no-args constructor. If it doesn't, you are going to get the error that you saw.

If you don't want the class to have a no-args constructor, then you could use the reflection API in a slightly more sophisticated way to find the right constructor and then call it.

So, is your problem now solved?
 
srinivas srinivasmeenavalli
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!