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

What is difference between == and === in js?

549


What do you mean by data type?

548


What is javac used for?

516


What is a cup of java?

559


What is difference between local variable and global variable?

474






Explain the differences between static and dynamic variables?

586


Where import statement is used in a java program?

610


What is ascii format?

550


What java ide should I use?

567


Explain the advantages of packages in java?

517


What is the map interface in java programming?

587


What is the synonym of string?

538


Can we overload the main() method?

551


What are advantages and disadvantages of OOPs?

630


How many bits is a double?

534