InterviewSolution
| 1. |
How is the ‘new’ operator different from the ‘newInstance()’ operator in java? |
|
Answer» Both ‘new’ and ‘newInstance()’ operators are USED to creating objects. The difference is- that when we ALREADY know the class name for which we have to create the object then we use a new OPERATOR. But suppose we don’t know the class name for which we need to create the object, Or we get the class name from the command line argument, or the database, or the file. Then in that CASE we use the ‘newInstance()’ operator. The ‘newInstance()’ keyword throws an exception that we need to HANDLE. It is because there are chances that the class definition doesn’t exist, and we get the class name from runtime. So it will throw an exception. |
|