Home > Java > javaTutorial > Java Constructors

Java Constructors

DDD
Release: 2025-01-06 03:47:40
Original
980 people have browsed it

Java Constructors:

Java constructors or constructors in Java is a terminology used to construct something in our programs. A constructor in Java is a special method that is used to initialize objects. The constructor is called when an object of a class is created. It can be used to set initial values for object attributes.

What are Constructors in Java?:

In Java, a Constructor is a block of codes similar to the method. It is called when an instance of the class is created. At the time of calling the constructor, memory for the object is allocated in the memory. It is a special type of method that is used to initialize the object. Every time an object is created using the new() keyword, at least one constructor is called.

Understanding how to effectively use constructors can significantly improve your Java programming skills, especially when you’re dealing with complex applications. It’s crucial to grasp the nuances of constructors to build scalable and maintainable software.

Example of Java Constructor:

// Driver Class
class Geeks {

    // Constructor
    Geeks()
    {
        super();
        System.out.println("Constructor Called");
    }

    // main function
    public static void main(String[] args)
    {
        Geeks geek = new Geeks();
    }
}
Copy after login
Copy after login

** How Java Constructors are Different From Java Methods?**

1.Constructors must have the same name as the class within which it is defined it is not necessary for the method in Java.
2.Constructors do not return any type while method(s) have the return type or void if does not return any value.
3.Constructors are called only once at the time of Object creation while method(s) can be called any number of times.

When Java Constructor is called?

Each time an object is created using a new() keyword, at least one constructor (it could be the default constructor) is invoked to assign initial values to the data members of the same class. Rules for writing constructors are as follows:

1.The constructor(s) of a class must have the same name as the class name in which it resides.
2.A constructor in Java can not be abstract, final, static, or Synchronized.
3.Access modifiers can be used in constructor declaration to control its access i.e which other class can call the constructor.

Types of Constructors in Java(TBD)

Now is the correct time to discuss the types of the constructor, so primarily there are three types of constructors in Java are mentioned below:

Java Constructors
3.copy constructor

Reference:https://www.geeksforgeeks.org/constructors-in-java/

Constructor overloading in Java:

In Java, we can overload constructors like methods. The constructor overloading can be defined as the concept of having more than one constructor with different parameters so that every constructor can perform a different task.

Here, we need to understand the purpose of constructor overloading. Sometimes, we need to use multiple constructors to initialize the different values of the class.

We must also notice that the java compiler invokes a default constructor when we do not use any constructor in the class. However, the default constructor is not invoked if we have used any constructor in the class, whether it is default or parameterized. In this case, the java compiler throws an exception saying the constructor is undefined.

Use of this () in constructor overloading:

However, we can use this keyword inside the constructor, which can be used to invoke the other constructor of the same class.

Example

// Driver Class
class Geeks {

    // Constructor
    Geeks()
    {
        super();
        System.out.println("Constructor Called");
    }

    // main function
    public static void main(String[] args)
    {
        Geeks geek = new Geeks();
    }
}
Copy after login
Copy after login

Reference:https://www.javatpoint.com/constructor-overloading-in-java

Program:

    public class Student {  
    //instance variables of the class  
    int id,passoutYear;  
    String name,contactNo,collegeName;  

    Student(String contactNo, String collegeName, int passoutYear){  
    this.contactNo = contactNo;  
    this.collegeName = collegeName;  
    this.passoutYear = passoutYear;  
    }  

    Student(int id, String name){  
    this("9899234455", "IIT Kanpur", 2018);  
    this.id = id;  
    this.name = name;  
    }  

    public static void main(String[] args) {  
    //object creation  
    Student s = new Student(101, "John");  
    System.out.println("Printing Student Information: \n");  
    System.out.println("Name: "+s.name+"\nId: "+s.id+"\nContact No.: "+s.contactNo+"\nCollege Name: "+s.contactNo+"\nPassing Year: "+s.passoutYear);  
    }  
    }  
Copy after login

Output:

public class SuperMarket
{
//class specific
static String name = "SB SuperMarket"; 
static int doorNo = 10; 
static boolean open = true; 
//non-static ---> Instance specific
String product_name; 
int price, discount; 

SuperMarket(String product_name, int price, int discount)
{
this.product_name = product_name; 
this.price = price; 
this.discount = discount; 
}

public static void main(String[] args)
{
SuperMarket product1 = new SuperMarket("cinthol", 22,2); 
SuperMarket product2 = new SuperMarket("biscuits",30,5);
SuperMarket product3 = new SuperMarket("cake",10,1); 
product1.sell();
product2.sell(); 
product3.sell();
product2.return_product(); 

}
public void return_product()
{
System.out.println("returning "+product_name);
}
public void sell()
{
System.out.println(product_name); 
System.out.println(price);
System.out.println(discount);
}
}

Copy after login

The above is the detailed content of Java Constructors. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template