Java new keyword

Java new keyword

  • It is used to create the object.
  • It allocates the memory at runtime.
  • It invokes the object constructor.
  • We can also use the new keyword to create the array object.
  • How to create a single object?
    • Syntax:
class_name object_name = new class_name();
    • Example:
MCA2 r1 = new MCA2();
  • How to create multiple objects?
    • Syntax:
class_name object_name1, object_name2, object_name3...;
    • Example:
MCA2 r1,r2,r3;
r1 = new MCA2();
r2 = new MCA2();
r3 = new MCA2();
  • How to create arrays of objects?
    • Array which store values like string, integer, Boolean, etc. 
    • Array of objects stores objects. 
    • The array elements store the location of reference variables of the object
    • Syntax:
class_name object_name[] = new class_name[size];
    • Example:
MCA2 obj[] = new MCA2[2] ;
Full Example:



Thanks a lot for query or your valuable suggestions related to the topic.

Previous Post Next Post

Contact Form