19 June 2023

Explain about Constructors In Java - learngreen.net

 Java constructors are a way to create things in our programs. A constructor is a special method that sets up objects in Java. The constructor is special method that happens when you make new from a class. It can be used to give starting values for things that belong to an object.


Explain about Constructors In Java?




In Java, Constructor is like a set of instructions just like a method. This happens when you make a new example of the class. When the constructor is called, space for the object is reserved in the computer's memory. It is a way to start the object in a special way. Each time something is made with the "new()" word, something called a constructor is used.




// Constructor Code Example
package Interview;
public class MyClass { 

//Constructor

	MyClass() {
		System.out.println("Constructor Invoked");
	}
  public static void main(String[] args) {
       MyClass obj = new MyClass();
    }
 }

// Output-> Constructor Invoked

What is the difference between Constructors and Methods?

In Java constructors has the same name as the Class from which it belongs to but incase of methods its not important that methods must posses the same name as the class. Constructors and methods are different. Constructors don't give any information back, but methods do. Methods either give back specific information or nothing at all. Constructors are used only once when creating an object, while methods can be used many times.


Importance of Constructor In Java:-

Constructors set the starting values for the parts of an object. This makes sure that the thing starts off okay and stays that way. Constructors can take in information to set values for an object when it is made. This makes creating objects better suited to what your program needs. Constructors make new objects from a class. When you say 'new', the computer sets aside space and sets it up for the object to start working. Java allows you to create many constructors in a class with different types of information. This makes it possible to create things with different ways to start them off. If you don't make any constructors in a class with specific instructions, Java will automatically create a default constructor. This sets the object to its starting values. If you make a constructor in a class, the default constructor won't be there anymore. Constructors are important when one thing takes after another thing. When you make a new class out of an existing one, everything in the old class is copied before anything new is added. Constructors  make things that work for your program and do what it needs.


Calling of Constructor:- 

When we make something new using the word "new", at least one constructor (which could be the basic one) is used to give the initial values to the parts of what we made. There are some guidelines to follow when writing constructors. The name of a class and the name of its constructor must be the same. In Java, you can't make a constructor abstract, final, static, or Synchronized. Access modifiers in a constructor decide which other class can use the constructor. We've learned that constructors are used to set up an object's starting position. A constructor is like a group of instructions, just like how methods also have their own set of instructions. These are steps that happen when an object is made.


Types of constructor in Java:-

Default constructor

Parameterized Constructor

Copy Constructor


Default Constructor:-

A constructor without any information is called the default constructor. You do not see a default constructor because it is hidden. If we don't write a constructor that takes any information, the computer won't make one for us automatically. It is removed. It's getting too much work and people are using a certain kind of constructor. The way the object is created has been changed from the default to a new way where specific values can be given as inputs. The default constructor cannot be changed by the parameterized constructor.




package Interview;
public class MyClass {
	//Default Constructor
	MyClass() {
		System.out.println("Default Constructor");
	}
  public static void main(String[] args) {
       MyClass obj = new MyClass();
    }
 }

// Output-> Default Constructor

Parameterized Constructor:-

A parameterized constructor is a constructor that needs some information to be created. If we want to start using specific values for the class fields, we can use something called a parameterized constructor.



package Interview;
public class MyClass {
	String message;
	int number;
MyClass(String message, int number) {
		this.message = message;
		this.number = number;
	}
  public static void main(String[] args) {
       MyClass obj = new MyClass("Java", 8);
       System.out.println(obj.message + " " + obj.number);
    }
 }

// Output-> Java 8

Copy Constructor:-

The copy constructor is different from other constructors because it makes a new object by copying data from another object.



package Interview;
public class MyClass {
	private String color;
	private int number;
	
public MyClass (String color, int number) {
		this.color=color;
		this.number = number;
	}
	
	public MyClass(MyClass next) {
		this.color = next.color;
		this.number = next.number;
	}

  public static void main (String[] args) {	
	MyClass obj = new MyClass("Yellow", 85);
	MyClass obj1 = new MyClass(obj);
	System.out.println(obj.color + " " + obj.number);
	System.out.println(obj1.color + " "+ obj1.number);
   }

 }
// Output-> Yellow 85
//          Yellow 85

/* Within the output, able to see that both obj
 * and obj1 have the same values for their color
 * and number properties. This confirms that the
 * copy constructor successfully made a new object
 * obj1 with the same quality values as the initial
 * object obj.
 */

 



No comments:

Post a Comment