Tuesday, August 20, 2013

Creating an Object: As mentioned previously a class provides the blueprints for objects. So basically an object is created from a class. In java the new key word 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 creating an object is given below:

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" );
   }
}

No comments:

Post a Comment