A Developer Gateway To IT World...

Techie Uncle Software Testing Core Java Java Spring C Programming Operating System HTML 5 Java 8 ES6 Project

Creating an Object:

Creating an Object in Java

How to create Object in Java?


As mentioned previously, a class provides the blueprints for objects. So, basically an object is created from a class. In java, the new keyword is used to create new objects.
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' key word 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 of create an object in Java:-






public class Puppy
{
 public Puppy(String name)
 {
  // This constructor has one parameter, name. 
  System.out.println("Passed Name is :" + name );
 }
 public static void main(String[] args)
 {
  // Following statement would create an object myPuppy 
  Puppy myPuppy = new Puppy( "tommy" );
 }
}


 



If we compile and run the above program then it would produce following result: 
Passed Name is :tommy

LEARN TUTORIALS

.

.