Constructors

Constructor is a special method that is used to initialize an instance of class. The constructor does not have a return type and has the same name as the class name.

A class can have several constructors with different types or number of arguments. One constructor can invoke another constructor in the first executable line via this keyword.

A constructor without arguments is called empty or a default constructor. If no constructor is specified, Java generates a default constructor.

You can define some static methods that use constructors with default argument values. They are also called factory methods.

In some cases will be better to use the creational patterns.

class Demo {
    public Demo(){...} // default constructor
    public Demo(ItemFactory itemFactory){...}
    public Demo(int arg1, int arg2){...}
    public Demo(int arg1){
       this(arg1,56);
       //...
    }

    //...
    
    public static Demo newStringDemo() {
        return new Demo(new StringItemFactory());
    }
}

Demo demo1 = new Demo();
Demo demo2 = new Demo(someItemFactory);