What is the output of the following Java program A17?

Description:

class Simple{  
 public Simple()  
 {  
   System.out.println("Constructor of Simple class is invoked");  
 }  
 void message(){System.out.println("Hello Java");}  
}  
  
class Test1{  
 public static void main(String args[]){  
  try{  
  Class c=Class.forName("Simple");  
  Simple s=(Simple)c.newInstance();  
  s.message();  
  }catch(Exception e){System.out.println(e);}  
 }  
}  

Solution:

Output

Constructor of Simple class is invoked
Hello Java

Explanation

The newInstance() method of the Class class is used to invoke the constructor at runtime. In this program, the instance of the Simple class is created.

0 answers