About "new" Keyword
Normally people always get confused with the usage of "new" in Java. Yes we do create objects with it but there's something more to it.
The "new" operator instantiates a class by allocating memory for a new object and returning a Reference to that memory. The new operator also invokes the object constructor. This reference is usually assigned to a variable of the appropriate type.
Concider this code:
MyObject obj;
obj = new MyObject();
In the first line you only create a Reference to an object of type MyObject. But that reference is blank and points nowhere! So if you tried doing something like obj.myFunction() your program would crash.
In the second line we use the "new" keyword to actually allocate an Instance of MyObject. Then we point our blank reference obj to that newly created instance. From there on, we can use obj to refer to that object and can use its member functions and properties like obj.myFuncion() and obj.myProperty = 2.
Creating an object of a Class without using "new" Keyword
It is of general practice to create an object with new keyword. It is also possible to create an object without new keyword. This is of logical significance only and not used in regular practice.
Normally people always get confused with the usage of "new" in Java. Yes we do create objects with it but there's something more to it.
The "new" operator instantiates a class by allocating memory for a new object and returning a Reference to that memory. The new operator also invokes the object constructor. This reference is usually assigned to a variable of the appropriate type.
Concider this code:
MyObject obj;
obj = new MyObject();
In the first line you only create a Reference to an object of type MyObject. But that reference is blank and points nowhere! So if you tried doing something like obj.myFunction() your program would crash.
In the second line we use the "new" keyword to actually allocate an Instance of MyObject. Then we point our blank reference obj to that newly created instance. From there on, we can use obj to refer to that object and can use its member functions and properties like obj.myFuncion() and obj.myProperty = 2.
Creating an object of a Class without using "new" Keyword
It is of general practice to create an object with new keyword. It is also possible to create an object without new keyword. This is of logical significance only and not used in regular practice.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | public class Hello { public void display() { System.out.println("Hello World"); } public static void main(String args[]) throws Exception { Class c1 = Class.forName("Hello"); // throws ClassNotFoundException Object obj1 = c1.newInstance( ); // throws InstantiationException and // IllegalAccessException Hello h1 = (Hello) obj1; h1.display(); } } |
forName() is a static method of java.lang.Class that loads the .class file of Hello into the RAM and returns the Hello as an object of class Class. c1 contains the reference of Hello. newInstance() method of class Class returns an object of Object class. Now, obj1 contains the reference of Hello. Internally, JVM may use new keyword. The object obj1 is explicitly type casted to Hello object, h1.