How to create an object from class in Java?


An object is created from a class using the new keyword. There are three steps when creating an object from a class −

  • Declaration − A variable declaration with a variable name with an object type.
  • Instantiation − The 'new' keyword is used to create the object.
  • Initialization − The 'new' keyword is followed by a call to a constructor. This call initializes the new object.

Example

public class Sample {
   public static void main(String args[]){
      Sample s = new Sample();
   }
}


Updated on: 19-Feb-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements