The term "Java constructors" refers to the process of constructing or building elements within our programs. In Java, a constructor is a special method utilized for initializing objects. When we instantiate a class object, the constructor is called that allow us to set initial values for the object's attributes.
In Java, a constructor is a code block that is smimilar to method but constructor name is same as class name . It is automatically called when a class instance is created, and it is critical in allocating memory for the object.
Essentially, a constructor serves as a unique type of method that initializes the object during its creation. Whenever the new() keyword is employed to create an object, at least one constructor is invoked.The Java compiler will automatically provide a default constructor if a class does not provide a specific constructor.
The term "constructor" originates from its function of constructing or building the object's value at the time of its creation. Declaring a constructor is not mandatory every time a class is created since the Java compiler will supply a default constructor when necessary.
// Java program using default constructor
public class Student { int Id; public Student() { System.out.println("Your ID is "+Id); } public static void main(String[] args) { Student student =new Student(); // when object is created constructor automatically invoked } }
Output:
0
// Java program using class and object
class Student{ public static void main(String args[]){ int x = 10; int y = 20; int z = addition(x, y); //Here x and y are actual parameters & add method is called System.out.println("The sum of x and y is = " + z); } public static int addition(int a, int b) //a and b are formal parameters // Here Method is static so that we can access without object { int sum; sum=a+b; return sum; //returning the sum } }
Output:
The sum of x and y is = 30
In the above example, we have used add method it is not already define so we have define it first and access in main method, we have made add method static because we can access static method without object. you wil learn about static method below this page
Method overloading refers to when a class has multiple methods with the same name but different parameter lists.
// Constructor Overloading program
class Addition{ Addition(int x,int y){ System.out.println("Sum of two number is : "+(x+y)); } Addition(int p,int q,int r){ System.out.println("Sum of three number is : "+(p+q+r)); } } class code_check{ public static void main(String args[]){ Addition addition=new Addition(12,13); Addition addition2=new Addition(12,13,15); } }
Output:
Sum of two number is : 25
Sum of three number is : 40
In Java, there are various methods available for copying the values of one object into another.
// Java program using class and object
class codeCheck{ int roll; String name; codeCheck(int roll, String name) { this.roll = roll; this.name = name; } codeCheck(codeCheck object) //constructor to initialize another object { roll = object.roll; // object is object of codeCheck class name =object.name; } void display(){ System.out.println(roll+" "+name); } public static void main(String args[]) { codeCheck code1 = new codeCheck(8650,"Dr Alok Raja"); codeCheck code2 = new codeCheck(code1); code1.display(); code2.display(); } }
Output:
8650 Dr Alok Raja
8650 Dr Alok Raja
Difference between constructor and method in Java are mention below
S.N | Constructor | Method |
---|---|---|
1. | In Object-oriented programming (OOPS) constructor is a special method that is used to initialize an object | A method is used to reveal an object's behaviour. |
2. | A return type is not permitted in a constructor. | A return type is required for every method. |
3. | When a object is created with the "new" keyword, the constructor is invoked implicitly. | Method needs to be invoked explicitly. |
4. | The Java compiler will automatically provide a default constructor if a class doesn't define one | The compiler in any situation does not supply the method. |
5. | The class name and constructor name are same. | The class name and the method name might or might not be the same. |
Post your comment