• Post Reply Bookmark Topic Watch Topic
  • New Topic

Object by newInstance()  RSS feed

 
AVA John
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What things are necessary for a class when its instance created at runtime ?[ Using Class.forName , new instance() ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

Any class can be loaded by Class.forName(). To create an instance of the class using the Class.newInstance() method, the class needs to have a no-argument (default) constructor. But the
classes in the java.lang.reflect package allow you to create objects of classes without default constructors, too -- it just takes a little more work.

There are access control issues too, of course; you can't use Class.newInstance() to create an instance of a class whose default constructor isn't accessible. Again, you may be able to use the "reflect" package to do so anyway.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!