New keyword in Java

Java Programming Object Oriented ProgrammingJava8

Yes, it is similar to a new keyword of C++. a new keyword is used to initialize/create an object. See the following example −

Employee employee = new Employee();

Here new keyword is used to create an object of class Employee.

new Employee() invokes the constructor of the class Employee.

new keyword can also be used without assigning the object to a reference variable. See the example −

String name = new Employee().getName();

Here we are creating an object using new keyword and then invoked a method getName() on the object and passed the result to a variable.

raja
Published on 23-Apr-2018 07:01:58
Advertisements