DEV Community

muthukumarasamy thangavel
muthukumarasamy thangavel

Posted on

Java Constructor

What is Constructor?
-->Constructor provides information about, and access to, a single constructor for a class.
-->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.
-->A class contains constructors that are invoked to create objects from the class blueprint.
-->Constructor declarations look like method declarations—except that they use the name of the class and have no return type.
-->A constructor does not have a return type, not even void.
-->Constructors can have parameters and can be overloaded.

-->Every Object call no argument constructor call in jdk.

Default Constructor:

//default Constructor 
public SuperMarke(){
        System.out.println("Are you constructor?");
}
Enter fullscreen mode Exit fullscreen mode

Argument Constructor:

//Argument Constructor 
public SuperMarket(String str , int i){
        System.out.println("Are you constructor?");

Enter fullscreen mode Exit fullscreen mode

No Argument constructor:

//No argument constructor or zero argument  const
public SuperMarket(){
        System.out.println("Are  you   0 zero  agrs constructor?");
Enter fullscreen mode Exit fullscreen mode

Advantages Constructor:

-->Constructors in Java play a crucial role in object-oriented programming by facilitating object instantiation and providing an opportunity for initialization.
-->They serve multiple purposes, allowing for code clarity, streamlined object management, and enhanced flexibility through overloading.

Causes:

-->Establishing initial state for objects is critical for predictable behavior.
Enter fullscreen mode Exit fullscreen mode

--> Java constructors can be overloaded, allowing different ways to create an object based on provided parameters.
--> Constructors help enforce encapsulation by allowing controlled access to the object's data.

Solutions:

--> Use constructors to initialize object fields directly upon creation, ensuring that all objects start in a valid state.
--> Leverage constructor overloading to provide multiple ways of instantiation, making your code more versatile and user-friendly.
--> Implement default constructors when no specific initialization is needed, providing a straightforward default state for your objects.

public class SuperMarket{

        static  String name = "Muthu";
        static int no  = 30;

        String  prod_name ;
        int price ;

//java must be create the data type mention the which datatype  
//Its called a constructor 
//public SuperMarket(String str, int i){
         // System.out.println("Are you constructor?");

//}
//default Constructor 
//Argument Constructor 
public SuperMarket(String prod_name , int  price){
// this dynamic keyword its refer to the current element
        this. prod_name= prod_name;
         this.price =  price;

}
  //No argument constructor or zero argument  const
//public SuperMarket(){
      //  System.out.println("Are  you   0 zero  agru constructor?");
//}
public static void main(String[] args){
//Its called as local variable 
   int i = 10;
//object create 
          SuperMarket product1 =    new SuperMarket("Boost",250);
         //  product1.prod_name = ;
       //    product1.price = 250;
           System.out.println (product1.prod_name);
           System.out.println (product1.price);

          SuperMarket product2 = new  SuperMarket("Horilks",300);
            //product2.prod_name = ;
           //  product2.price = 300;
          System.out.println(product2.prod_name);
          System.out.println(product2.price);

      //   SuperMarket product3 = new  SuperMarket();

}
}
Enter fullscreen mode Exit fullscreen mode

Output:

Top comments (0)