Constructor Basics
Constructor is special kind of method. Its name must be a class name.
class Foo {
Foo() { } // The constructor for the Foo class
}
Notice what’s missing? There’s no return type!
constructor has no return type
Typically, constructors are used to initialize instance variable state, as follows: class Foo
{
int age;
String name;
Foo( ){
name = “ABC”;
age = 23;
}
}The constructor will automatically invoke when an object is created.
Foo f = new Foo(); // Empty Constructor calling
class Foo {
Foo() { } // The constructor for the Foo class
}
Notice what’s missing? There’s no return type!
constructor has no return type
Typically, constructors are used to initialize instance variable state, as follows: class Foo
{
int age;
String name;
Foo( ){
name = “ABC”;
age = 23;
}
}The constructor will automatically invoke when an object is created.
Foo f = new Foo(); // Empty Constructor calling