Java new Keyword

In Java, objects are created with "new" keyword. " Java new" allocates memory to the newly created object at runtime.
Observe the following on Java new Keyword, then we will go into details.
public class Employee
{
  int salary = 5000;
  public static void main(String args[])
  {
    Employee emp1 = new Employee();
    System.out.println("Employee Salary Rs."+emp1.salary);
  }
}

Java new KeywordOutput Screenshot on Java new Keyword

Employee emp1 = new Employee();

By program wise it it simple code. emp1 is an object of Employee class. The Java new keyword allocates memory for the newly created object emp1. Generally an object takes 4 bytes of memory. The constructor Employee(), creates an object of Employee but without name, The nameless object created by the constructor and returned is given name called emp1. With emp1 you call call a method or instance variable etc.

Finally to say, the constructor creates an object, new allocates memory for the newly created object.

More detailed explanation is available on Java new in the following two tutorials clearly with Programs and Screenshots.

1. Java Reference Variables – Objects – Anonymous objects
2. First Java Program

2 thoughts on “Java new Keyword”

Leave a Comment

Your email address will not be published.