Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

newInstance() method

 
Simon Xu
Ranch Hand
Posts: 235
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
I have two classes C1 extends C2

When I use
Object instance1=C1.newInstance();
it gives me java.lang.InstantiationException
What is wrong? Please help,
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't know so I looked it up:
direct from Sun...
-----
public class InstantiationException
extends Exception
Thrown when an application tries to create an instance of a class using the newInstance method in class Class, but the specified class object cannot be instantiated because it is an interface or is an abstract class.
------
I would check your classes and make sure they are all clases nor interfaces or abstract.
hope that helps
Dave
 
Simon Xu
Ranch Hand
Posts: 235
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, Dave,
Thanks for your prompt response.
C1 and C2 are concrete classes. The question is that if I delete super(); it is OK. what's wrong?

Please help,
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a default constructor in C2?
that's the only thing that comes to mind quickly, sorry I can't be more helpful but I'm fairly new at Java. If you want to post some code I'd be happy to take a look at it for you.
Dave
 
Simon Xu
Ranch Hand
Posts: 235
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, Dave,
It is not the default one. In fact, I am implementing a system which accept the input data from the user (argument type, value, and return type), the system organizes them into a testing program, and dynamically execute each java class in class library and compare the result with user's expected. public class
C1 class is here

C2 is this one

In the main,I use
String className="Isoceles"; //this is example
Class thisClass=Class.forName(className);
Object instance1=thisClass.newInstance();
it gives me java.lang.InstantiationException
What is wrong? Please help,
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need an object of Class Class (don't you love it ) to use the newInstance method on.
Try
Object instance1=Class.forName("C1").newInstance();
 
Simon Xu
Ranch Hand
Posts: 235
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, Cindy,
Thank for your reply.
It was a typo. I did use
Class thisClass=Class.forName(className);
Object instance1=thisClass.newInstance();
Please check it for me.
Simon
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It works fine for me. Of course, a Shape class is necessary as well - I used
<code><pre>class Shape {
private String name;
public Shape() {}
public Shape(String name) {
this.name = name;
}
}
</pre></code>
as a minimal example.
I can't see any reason for this error. Is there any message associated with the exception? Did you double-check the line number from the stack dump to verify that it comes from the c1.newInstance() line? Also, try using this instead to see if you get the same error:
<code><pre>
Object instance0 = Isoceles.class.newInstance();
</pre></code>
Incidentally your formula for Area of a triangle is incorrect, though that should have nothing to do with your mysterious problem.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic