Sometimes in a constructor, no statement is given. What does that indicate? For example if i create a class CIRCLE, then inside the class i write CIRCLE() {}, that is nothing is written inside. Can anyone explain it?
-
It's a simple, parameterless, empty bodied constructor. Do you know what a constructor is? – Sotirios Delimanolis Jun 21 '14 at 17:21
-
2It's useful when you want to restrict access (private, protected or "package") to the constructor. – fabian Jun 21 '14 at 17:25
-
2See this answer : http://stackoverflow.com/a/4488766/2359424 – masinger Jun 21 '14 at 17:32
-
no effort has been made to learn the language – saurabh kumar Jun 21 '14 at 17:49
-
Excuse me mister, i am still learning so stop making stupid comments if you dont want to help me – raiyan106 Jul 05 '14 at 16:23
4 Answers
If your question is "why would anyone write such a constructor", then the answer is that the no-args default constructor only exists if no other constructor is specified.
Consider the following class.
class Foo {
int x;
}
As written, someone could write the following code to construct Foo
.
Foo foo = new Foo();
However, now suppose I added a constructor which takes arguments.
class Foo {
int x;
public Foo(int x) {
this.x = x;
}
}
Now, suddenly, Foo foo = new Foo();
no longer works. To restore it, I must add the empty constructor again.
class Foo {
int x;
public Foo(int x) {
this.x = x;
}
public Foo() { }
}
Now, What if there are no other constructors that take arguments?
In that case, it is generally as the other answers suggest, to restrict access to constructing the class.
In the following definition of Foo
, nobody is allowed to construct Foo
. Perhaps Foo
is meant only as a static class.
class Foo {
int x;
private Foo() { }
}
In the protected
case, only subclasses
can construct Foo
.
class Foo {
int x;
protected Foo() { }
}

- 71,677
- 44
- 195
- 329
-
I disagree, how come `Foo foo=new Foo();` no longer works!!! It's the default constructor which is called because of `super` inheritance model. – Am_I_Helpful Jun 21 '14 at 17:37
-
1@shekharsuman, this is not a matter of opinion. Please try it out for yourself. – merlin2011 Jun 21 '14 at 17:52
-
1@shekharsuman because if we declare a parameterized constructor then compiler is not going to provide the default construtor – saurabh kumar Jun 21 '14 at 17:52
If nothing is written, then when a new Object of that type is created, nothing 'extra' is done, whereas if in the constructor has code in, it does something.
For example, the following consructor for a class called 'Bank' assigns the argument 'name' to the field 'bankName', then instantiates a Terminal and 2 bank accounts:
private static final int INITIAL_BALANCE = 200;
public Bank( String name )
{
bankName = name;
atm = new Terminal();
account1 = new BankAccount( INITIAL_BALANCE );
account2 = new BankAccount( INITIAL_BALANCE );
}

- 1,871
- 3
- 17
- 32
If there is no code in the constructor, chances are, it was declared to change the access to the constructor. By default, constructors are public. If you wanted to make it private, protected or package-private, you must explicitly declare it and manually change the modifier.
class Example {
public static void main(String[] args) {
new Demo(); //this is currently allowed
}
}
class Demo {
}
In order to prevent the creation of a Demo object within Example, we could declare Demo's constructor amd make it private:
class Demo {
private Demo() { }
}
Another reason could be that the class has a constructor that requires parameters. If so, you must explicitly declare the no-arg constructor to be able to use it.

- 14,470
- 7
- 39
- 84
It's a default constructor. For instance if you go:
Circle circle = new Circle();
You are then calling the default constructor. When you go ... Circle()
that is a call to the default constructor, the one with no parameters.
The point of this is just to 'construct' an object or instantiate a class (instantiate just means create an object which is an instance of the class) with no additional information i.e. parameters.
This would generally be used to initialize fields to their default values, like so:
public Circle() {
this.x = 0;
this.y = 0;
}

- 1,735
- 4
- 28
- 56