Wednesday, February 22, 2012

JAVA CLASS CONSTRUCTOR

JAVA CONSTRUCTOR
  • A class contains constructors that are invoked to create objects from the class blueprint.
  • A constructor is a special method that is used to initialize a newly created object and is called just after the memory is allocated for the object.
  • It can be used to initialize the objects ,to required ,or default valuesat the time of object creation It is not mandatory for the coder to write a constructor for the class
If no user defined constructor is provided for a class, compiler initializes member variables to its default values.
  • numeric data types are set to 0
  • char data types values are null
CONTRUCTOR DESCRIPTION:
  • Constructors are used to assign initial values to instance variables of the class.
  • A default constructor with no arguments will be called automatically by the Java Virtual Machine (JVM).
  • Constructor is always called by new operator. Constructors are declared just like as we declare methods, except that the constructor don't have any return type.
In order to create a Constructor observe the following rules
  • It has the same name as the class
  • It should not return a value not even void
  • You don't have to provide any constructors for your class, but you must be careful when doing this.
  • The compiler automatically provides a no-argument, default constructor for any class without constructors.
  • This default constructor will call the no-argument constructor of the superclass. In this situation, the compiler will complain if the superclass doesn't have a no-argument constructor so you must verify that it does.
  • If your class has no explicit superclass, then it has an implicit superclass of Object, which does have a no-argument constructor.

Example:
class Sample_constructor
{
public static void main(String args[])
{
Sample_constructor sam=new Sample_constructor();
//object is reated &
//constructor is invoked using new
System.out.Println("Hai ...")
}
}

OUTPUT:
Hai...

NOTE:
· Though there is no constructor definition, Java defaultly does the contructor process when method is invoked using new operator with tha same name of class.
· Here there in this example there is no construction defitions.but when code new Sample_contructor() is reached  it creates & initializes object

1 comment:

  1. As reported by Stanford Medical, It's really the ONLY reason women in this country live 10 years more and weigh 42 pounds lighter than we do.

    (And realistically, it is not related to genetics or some secret exercise and absolutely EVERYTHING to "how" they eat.)

    P.S, I said "HOW", not "what"...

    CLICK on this link to discover if this easy quiz can help you release your real weight loss possibilities

    ReplyDelete