Skip to main content

What is Constructor in Java?

In java constructor name always same as class name. in java, we use a constructor for initializing object property when an object is created. this means that when we create an object using the new operator.
at that time u want to assign some property to the object then we use constructor.

example-
          // if u have class Fruit mean u have one object Fruit
         public  class Fruit{   
           
               public Fruit(){ // This is our constructor with name Fruit

                 }    
                 } 

in this example, we will be creating one default constructor.




Comments