If you want to know about constructors then you need to join the Best Core Java training in Pune.
Constructor:
The newly created object is initialized by the constructor which looks like an instance method in Java. This does not have a return type, therefore, it is not a method. This is referred to as a special type of method in Java.
The name of the constructor is the same as the class name and this is how it looks like:
public class MyClass{ //This is the constructor MyClass(){ } .. }
Join the top Java classes in Pune and know more about this topic.
Working of a Constructor:
Here is an example to understand the working of a constructor MY CLASS.
This is how we create the object of my class:
MyClass obj = new MyClass()
The object of class My Class is created by this new keyword and it calls the constructor for initialing this object that has been created newly.
A simple constructor program in Java
Let us look at simple constructor programs in Java with an object obj of class, Hello and then the instance variable name of the object has been displayed. The object obj was created after which the constructor got invoked. Here we will see that the keyword used refers to the current object obj in this example:
public class Hello { String name; //Constructor Hello(){ this.name = "BeginnersBook.com"; } public static void main(String[] args) { Hello obj = new Hello(); System.out.println(obj.name); } }
Output:
Types of Constructors
Let us see various types of constructors:
1) Default
2) No-arg constructor
3) Parameterized
Default constructor
If your class does not contain include any constructor then Java compiler projects a default constructor in your code. This is called as default constructor. It will be inserted only at the time of compilation. The below diagram shows this process:
A default constructor cannot be received if you implement a default constructor.
No-arg constructor:
No-arg constructor is where you cannot find or not given any arguments. A default constructor is similar to a signature. Any code can be present in the body of the constructor, unlike default constructor.
class Demo { public Demo() { System.out.println("This is a no argument constructor"); } public static void main(String args[]) { new Demo(); } }
Output:
This is a no argument constructor
Parameterized constructor
This type of constructor with arguments is termed as parameterized constructors.
Example: parameterized constructor.
There are two parameters in the below example and they are id and name. After creating objects obj1 and obj2, two arguments have been passed to this constructor for invoking the constructor.
public class Employee { int empId; String empName; //parameterized constructor with two parameters Employee(int id, String name){ this.empId = id; this.empName = name; } void info(){ System.out.println("Id: "+empId+" Name: "+empName); } public static void main(String args[]){ Employee obj1 = new Employee(10245,"Chaitanya"); Employee obj2 = new Employee(92232,"Negan"); obj1.info(); obj2.info(); } }
Output:
Id: 10245 Name: Chaitanya Id: 92232 Name: Negan
Constructor Chaining
Constructor chaining happens when a constructor invokes another constructor of the same class.
Super()
The constructor of a parent class is invoked when a child class constructor is invoked.
class MyParentClass { MyParentClass(){ System.out.println("MyParentClass Constructor"); } } class MyChildClass extends MyParentClass{ MyChildClass() { System.out.println("MyChildClass Constructor"); } public static void main(String args[]) { new MyChildClass(); } }
Output:
MyParentClass Constructor MyChildClass Constructor
Constructor Overloading
When two or more constructors with the same name have different parameter list then it is called as Constructor Overloading.
Java Copy Constructor
When the values of an object are copied to another object then it is called a copy constructor.
class JavaExample{ String web; JavaExample(String w){ web = w; } /* This is the Copy Constructor, it * copies the values of one object * to the another object (the object * that invokes this constructor) */ JavaExample(JavaExample je){ web = je.web; } void disp(){ System.out.println("Website: "+web); } public static void main(String args[]){ JavaExample obj1 = new JavaExample("BeginnersBook"); /* Passing the object as an argument to the constructor * This will invoke the copy constructor */ JavaExample obj2 = new JavaExample(obj1); obj1.disp(); obj2.disp(); } }
Output:
Website: BeginnersBook Website: BeginnersBook
The Java training institutes in Mumbai teaches you in detail about the Java Constructor from the basics.
Reference site: beginners book
Authorname: Chaitanya singh
Thanks for sharing this nice tutorial.