There are 2 different ways to create an object. a)By using
keyword "new" b)By using Class.forName
("className").newInstance();
What is the difference between these 2 methods.

Answer Posted / qim2010

Java’s “new” operator is used to load the class statically.

class MyClass {
public static void main(String args[]) {
Car c = new Car();
}
}

Class.forName (String className); is used for dynamic class
loading. The string className can be supplied dynamically at
run time. Once the class is dynamically loaded the following
method returns an instance of the loaded class. It’s just
like creating a class object with no arguments.

class.newInstance (); //A non-static method, which creates
an instance of a
//class (i.e. creates an object).

Is This Answer Correct ?    4 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Explain the features of interfaces in java?

746


Why we use protected in java?

721


What is factor r?

725


What are static blocks in java ?

817


Can memory leak happen java?

778


What is parsing and its types?

774


What are default methods ?

772


How do you add spaces in java?

723


Name some OOPS Concepts in Java?

822


What is difference between jdk,jre and jvm?

775


Can a constructor call another constructor?

755


How do you call a reference in java?

688


Why is singleton not thread safe?

825


What is the impact of declaring a method as final?

744


What is a dynamic array in java?

785