CONSTRUCTOR S IN JAVA

Constructor Declarations:

The purpose of a constructor in class C is to initialize new objects (instances) of the class. A
constructor-declaration in class C has the form

constructor-modifiers C(formal-list) throws-clause constructor-body


The constructor-modifiers may be a list of at most one of private, protected, and public a constructor cannot be abstract, final, or static. A constructor has no return type.Constructors may be overloaded in the same way as methods:constructor signature (a list of the parameter types in formal-list) is used to distinguish constructors in the same class. A constructor may call another overloaded constructor in the same class using the syntax.

this(actual-list) but a constructor may not call itself, directly or indirectly. A call this(...) to another constructor, if present, must be the very first action of a constructor, preceding any declaration or statement.

The constructor-body is a block-statement and so may contain statements as well as declarations of variables and local classes. The constructor-body may contain return statements, but no return statement can take an expression argument.


A class that does not explicitly declare a constructor implicitly declares a public, argument less default constructor whose only (implicit) action is to call the superclass constructor

public C() { super(); }

The throws-clause of the constructor specifies the checked exceptions that may be thrown by the constructor,in the same manner as for methods .When new creates a new object in memory , the object's non static fields are given default initial values according to their type. Then a constructor is called to further initialize the object, and the following happens: First,some super class constructor is called exactly once, then the non static field initializers and non static initializer blocks are executed once in order of appearance in the class declaration, and finally the constructor body (except the explicit superclass constructor call, if any) is executed. The call to a superclass constructor will cause a call to a constructor in its superclass, and so on, until reaching Object().

Overloaded constructor:

class Point {
int x, y;
Point(int x, int y) { Overloaded constructor
this.x = x;
this.y = y; }

Point(Point p) Overloaded constructor
{
this(p.x, p.y); }
Calls the first constructor

void move(int dx, int dy)
{
x += dx;
y += dy; }
public String toString()
{ return "(" + x + ", " + y + ")"; }
}

Calling a Superclass Constructor using the syntax super(x, y).

Initializer Blocks:

In addition to field initializers , a class may contain initializer-blocks. Initializer blocks may
be used when field initializers or constructors do not suffice. We use the term initializer to me an field initializers as well as initializer blocks. A static initializer block has the form
static block-statement
.

The static initializer blocks and field initializers of static fields are executed, in order of appearance in
the class declaration, when the class is loaded. A non static initializer block is simply a free-standing block-statement. Non static initializer blocks are executed after the constructor when an object is created.

An initializer is not allowed to throw a checked exception. If execution of a static initializer throws an (unchecked) exception during class loading, that exception is discarded and the exception ExceptionInInitializerError is thrown instead.

RELATED POST

FIELD DECLARATION IN JAVA

No comments:

Post a Comment