PDA

View Full Version : What is constructor in Java?


pratul
07-02-2010, 12:19 PM
A constructor is a special member function which is automatically invoked whenever the object of the class is created.

1. A constructor has the same name as the class name.
2. There can be more than one constructors in a class.
3. A constructor doesn't have the return type.
4. A constructor can be overloaded.
5.Generally declared in the public section.
6. Constructors are always invoked in the order of their derivation.

Example:


class DemoConstructor
{
private int a;
private int b;

pubic DemoConstructor()
{
a=b=0;
}

pubic DemoConstructor(int a, int b)
{
a=(a>80?80:(a<0?0:a));

b=(b>30?30:(b<0?0:b));

}

public int getA()
{
return a;
}


public int getB()
{
return b;
}

}

class DemoCon {

public static void main(String args[])
{

int w,x,y,z;

DemoConstructor dc1 = new DemoConstructor();
DemoConstructor dc2 = new DemoConstructor(25, 45);

w = dc1.getA()
x = dc1. getB();
y = dc2.getA()
z = dc2. getB();


System,out.println("The value of w is :" +w);
System,out.println("The value of x is :" +x);
System,out.println("The value of y is :" +y);
System,out.println("The value of z is :" +z);
}
}


Explanation: DemoConstructor dc1 = new DemoConstructor();

when this is created then default constructor is invoked that is the constructor which does not take any parameters.

DemoConstructor dc2 = new DemoConstructor(25, 45);

When this is created then the constructor with parameters is invoked and it works accordingly.

lencemark
22-06-2010, 09:45 AM
A constructor creates an Object of the class that it is in by initializing all the instance variables and creating a place in memory to hold the Object. It is always used with the keyword new and then the Class name. For instance, new String(); constructs a new String object.
Sometimes in a few classes you may have to initialize a few of the variables to values apart from their predefined data type specific values. If java initializes variables it would default them to their variable type specific values. For example you may want to initialize an integer variable to 10 or 20 based on a certain condition, when your class is created. In such a case you cannot hard code the value during variable declaration. such kind of code can be placed inside the constructor so that the initialization would happen when the class is instantiated.

abhishek
27-06-2010, 06:43 PM
Nice explanation.

jhonybush
28-08-2010, 10:27 PM
A constructor creates an Object of the class that it is in by initializing all the instance variables and creating a place in memory to hold the Object. It is always used with the keyword new and then the Class name. Constructor’s syntax does not include a return type, since constructors never return a value.

jennypatel
30-08-2010, 05:51 PM
A constructor in Java is created in order to initialize the members of object class automatically. Constructor is called automatically when we declare object of any class. We have default constructor for each class.

seglob111
29-09-2010, 02:20 PM
When you create a new instance (a new object) of a class using the new keyword, a constructor for that class is called. Constructors are used to initialize the instance variables (fields) of an object. Constructors are similar to methods, but with some important differences.
Constructor name is class name. A constructors must have the same name as the class its in.
Default constructor. If you don't define a constructor for a class, a default parameterless constructor is automatically created by the compiler. The default constructor calls the default parent constructor (super()) and initializes all instance variables to default value (zero for numeric types, null for object references, and false for booleans).
Default constructor is created only if there are no constructors. If you define any constructor for your class, no default constructor is automatically created.

-----------------------------------------------------------------------------------------------------------------------------------
Earn an Extra $1000 to $1200 per month doing Part Time Data Entry Jobs! Work from home data entry jobs to post simple data submissions on Internet. Make $1 per entry. Easy form filling, data entry and ad posting jobs. No selling, No phone calls, No Marketing. No Investment. Bi-weekly payments. Full Training Provided. Pls visit: Data-Entry (http://www.dataentrywork.net/?id=26201)

jameserics
15-10-2010, 04:46 PM
The constructor initializes all the instance variables and objects stored in memory by creating a place to create an object of the class. It is always used in conjunction with the new keyword is the name of the class.