Once we create an object compulsory we should perform initialization then only the object in a position to properly.
When ever creating an object some piece of code executed automatically to perform initialization of the object, this piece of the code is nothing but constructor.
Hence, the main purpose of constructor is to perform initialization of an object.
eg-:
class Constructor{
String greet;
int review;
Constructor(String greet,int review){
this.greet=greet;
this.review=review;
}
public static void main(String[] args){
Constructor c=new Constructor("Hello",3);
Constructor c1=new Constructor("Hi",4);
}
}
String greet;
int review;
Constructor(String greet,int review){
this.greet=greet;
this.review=review;
}
public static void main(String[] args){
Constructor c=new Constructor("Hello",3);
Constructor c1=new Constructor("Hi",4);
}
}
explanation of above code-:
This Java code defines a class named `Constructor` and demonstrates the use of a constructor to initialize instances of this class. Let's break down the code step by step:
1. Class Definition:
class Constructor {
This line starts the definition of a Java class named `Constructor`.
2. Instance Variables:
String greet;
int review;
Inside the class, two instance variables are declared:
- `greet`: A String variable to store a greeting message.
- `review`: An integer variable to store a review rating.
3. Constructor:
Constructor(String greet, int review) {
this.greet = greet;
this.review = review;
}
This is a constructor for the `Constructor` class. Constructors are special methods used for initializing objects of a class. In this case, the constructor takes two parameters: a `String` called `greet` and an `int` called `review`. Inside the constructor:
- `this.greet = greet;` assigns the value of the `greet` parameter to the `greet` instance variable of the object being created.
- `this.review = review;` assigns the value of the `review` parameter to the `review` instance variable of the object being created.
4. `main` Method:
public static void main(String[] args) {
This is the `main` method, the entry point of the Java program.
5. Object Creation:
Constructor c = new Constructor("Hello", 3);
Constructor c1 = new Constructor("Hi", 4);
Inside the `main` method, two objects of the `Constructor` class are created:
- `c`: An instance of the `Constructor` class is created with the values "Hello" for `greet` and 3 for `review`.
- `c1`: Another instance of the `Constructor` class is created with the values "Hi" for `greet` and 4 for `review`.
In summary, this code defines a class called `Constructor` with two instance variables and a constructor to initialize them. It then creates two objects of this class and assigns values to their instance variables using the constructor. This demonstrates the basic concept of object-oriented programming in Java, where you define a class with its attributes and behaviour (in this case, just initialization) and then create instances (objects) of that class to work with data.
Difference Between Constructor and Instance block-:
The main purpose of constructor is to perform initialization of an object.
- But other than initialization, if we want to perform any activity for every object creation, then we should go for instance block. For example, updating one entry in the database for every object creation or incrementing a count value for every object creation.
- Both constructor and instance block have their want different purpose, and replacing one concept with another concept may not work always.
- Both constructor and instance block will be executed for every object creation, but instance block first followed by constructor.
- Return type concept not applicable for constructor, even void also.
By mistake declare return type for the constructor, we won't get any compile time error beacuse compiler treats it as a method.
class Test{
void Test(){
System.out.print("hello");
}
public static void main(String[] args){
Test t=new Test();
}
}
2. Constructor:
void Test() {
System.out.print("hello");
}
This part of the code defines a method named `Test`. However, it's important to note that this is not a typical constructor. In Java, a constructor has the same name as the class and doesn't have a return type (not even `void`). This method does have a return type (`void`) and thus is not a constructor. It's just an ordinary instance method with the same name as the class. When you create an object of this class, this method can be called like any other method.
Which modifier applicable for constructor ?
The only applicable modifier for constructor are public,private,protected,default if we are trying to use any other modifier we will get compile time error.
Default Constructor
Compiler is responsible to generate default constructor but not JVM.
if we are not writing any constructor then only compiler will generate default constructor that is if we are writing at least one constructor then compiler won't generate default constructor hence every class in Java can contain constructor it may be default constructor generated by compiler are customizer constructor explicitly provided by programmer but not both simultaneously.
Prototype of default constructor -:
- It is always no-arg constructor.
- The access modifier of default constructor is exactly same as class (this rule applicable only for public and default).
- It contains only one line super(); it is a no argument call to super class constructor.
Overloaded Constructor
within a class we can declare multiple constructors and all these constructors having same name but different type of arguments hence all these constructors consider as overloaded constructor hence overloading concept applicable for constructor.
eg-:
class Test{
Test(){
}
Test(int i){
}
Test(long l){
}
Test(double d){
}
public static void main(String[] args){
Test t=new Test();
Test t1=new Test(2);
Test t2=new Test(5l);
Test t3 =new Test(3.002);
}
}
Explanation of above code ?
The provided Java code defines a class named `Test` with multiple constructors and a `main` method. Let's go through the code step by step:
1. Class Declaration:
class Test {
This line declares a class named `Test`. Inside this class, there are several constructors with different parameter types.
2. Constructors:
The class `Test` has multiple constructors with different parameter types:
- Default Constructor:
Test() {
}
This constructor takes no parameters and is the default constructor. It doesn't perform any specific actions when an object is created using it.
- Constructor with an `int` parameter:
Test(int i) {
}
This constructor takes an `int` parameter (`i`) but doesn't perform any specific actions when an object is created using it.
- Constructor with a `long` parameter:
Test(long l) {
}
This constructor takes a `long` parameter (`l`) but doesn't perform any specific actions when an object is created using it.
- Constructor with a `double` parameter:
Test(double d) {
}
This constructor takes a `double` parameter (`d`) but doesn't perform any specific actions when an object is created using it.
3. `main` Method:
public static void main(String[] args) {
Test t = new Test();
Test t1 = new Test(2);
Test t2 = new Test(5l);
Test t3 = new Test(3.002);
}
This part of the code defines the `main` method, which serves as the entry point of the Java program. Inside the `main` method, several instances of the `Test` class are created using different constructors:
- `t` is created using the default constructor, which takes no arguments.
- `t1` is created using the constructor that accepts an `int` argument and is passed the value `2`.
- `t2` is created using the constructor that accepts a `long` argument and is passed the value `5l`.
- `t3` is created using the constructor that accepts a `double` argument and is passed the value `3.002`.
However, none of these constructors have specific actions defined within them, so when the objects are created, they do not perform any additional actions beyond object creation.
In summary, this code demonstrates constructor overloading, where a class has multiple constructors with different parameter types, but the constructors themselves don't perform any meaningful actions. When objects of the `Test` class are created, they simply instantiate the objects without any side effects.
Important point-;
- For constructor inheritance and overriding concept not applicable but overloading concept is applicable.
- Every class in Java, including an abstract class can contain constructor, but an interface can't contain constructor.
eg-:
class Test{
Test(){
}
}
} //allowed
abstract class Test{
Test(){
}
}//allowed
interface Test{
Test(){
}
}//not allowed
case 1-: recursive method call is run time exception is saying stack-overflow error.
but in our program there is chance of recursive constructor then code won't compile and we will get compile time error.
eg-:
class Test{
Test(){
this(10);
}
}
Test(int i){
this();
}
public static void main(String[] args){
System.out.print("Hello");
}
}
compile time error Recursive constructor invocation