constructor

In Java, a constructor is a special method that initializes objects when they are created.

Prerequisites

  1. Should know about local and global variables
  2. global variables to non static variables
  3. default value of global variables
  4. return data types

1.Constructor will be called automatically when objects are created
2.Constructor names should be a class names
3.constructor will not have any return data types

What are the 3 types of constructor?
1.Default
2.Parameterized
3.Copy constructors

What is default constructor

A default constructor in Java is a constructor automatically provided by the compiler if no constructors are explicitly defined in a class. It's a parameter-less (no argument) constructor that initializes instance variables to their default values (e.g., 0 for integers, null for objects, false for booleans.

what is Parameterized constructor in java

A parameterized constructor in Java is a constructor that takes one or more arguments (parameters) during object creation. These parameters allow you to specify the initial values of the object's instance variables when the object is created. This contrasts with the default constructor, which takes no arguments and initializes instance variables with default values.

what is Copy constructors constructor in java

A copy constructor in a Java class is a constructor that creates an object using another object of the same Java class. That's helpful when we want to copy a complex object that has several fields, or when we want to make a deep copy of an existing object.

When constructor is called automatically

Yes, constructors are automatically called when a new object of a class is created. They are special methods used to initialize the object's state

Constructor overloading in Java

Constructor overloading in Java allows a class to have multiple constructors with different parameter lists, all having the same name as the class. This enables the creation of objects with varying initial states, based on the arguments provided during object instantiation.

What is this keyword in Java?

The this keyword in Java is a reference variable that refers to the current object. It is used within an instance method or a constructor to access members of the current object such as instance variables, methods, and constructors.

Example:

public class SuperMarket {

  String product_name;
  int price;
  int discount;
  private Object buy;


  public SuperMarket(String string, int i, int j) {
    // TODO Auto-generated constructor stub
    product_name = string;
    price = i;
    discount = j;  


  }
  public SuperMarket(String string, int i) {
    // TODO Auto-generated constructor stub
    product_name = string;
    price = i;
  }
  public SuperMarket() {
    // TODO Auto-generated constructor stub
    System.out.println("Material from London");
  }
  public static void main(String[] args) {
    SuperMarket product1 = new SuperMarket("maida", 90,8);
    SuperMarket product2 = new SuperMarket("tender_coconut", 45,2);
    SuperMarket product3 = new SuperMarket("maggi", 45); 
    SuperMarket product4 = new SuperMarket();



    System.out.println(product1.product_name  );
    System.out.println(product2.product_name );
    product1.buy();
}

public void buy() { 
  System.out.println(product_name + -price);
}
}

Output:

Material from London
maida
tender_coconut
maida-90