What is a constructor?

A constructor is a "kind of" method in java which is used to initialize objects. The constructor is called when an object of a class is created. It can be used to set initial values for object attributes. A constructor definition is similar to a method definition, except that the constructor should have same name as the class and doesn't contain a return type.

public class MyClass {
    public MyClass() {
      // class level definitions
    }
}

A constructor is a "kind of" method in java which is used to initialize objects. The constructor is called when an object of a class is created. It can be used to set initial values for object attributes. A constructor definition is similar to a method definition, except that the constructor should have same name as the class and doesn’t contain a return type.

public class MyClass {
    public MyClass() {
      // class level definitions
    }
}

Buy Me A Coffee

Found this article helpful? Please consider supporting!

Ram
Ram

I'm a full-stack developer and a software enthusiast who likes to play around with cloud and tech stack out of curiosity. You can connect with me on Medium, Twitter or LinkedIn.

Leave a Reply

Your email address will not be published. Required fields are marked *