declare and instantiate an object java code example

Example 1: how to initialize 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" );
   }
}

Example 2: instantiate a class java

When you create an object, you are creating an instance of a class, therefore "instantiating" a class

Tags:

Misc Example