Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in Java by (4k points)
What is the difference between Class.forName() and Class.forName().newInstance()?

I do not understand the significant difference (I have read something about them!). Could you please help me?

1 Answer

0 votes
by (46k points)
Class.forName() gives you the class object, which is useful for reflection. The methods that this object has are defined by Java, not by the programmer writing the class. They are the same for every class. Calling newInstance() on that gives you an instance of that class (i.e. calling Class.forName("ExampleClass").newInstance() it is equivalent to calling new ExampleClass()), on which you can call the methods that the class defines, access the visible fields etc.

Related questions

0 votes
1 answer
asked Nov 25, 2019 in Java by Anvi (10.2k points)
0 votes
1 answer
asked Dec 2, 2019 in Java by Anvi (10.2k points)
0 votes
1 answer

Browse Categories

...