How to create a private constructor in Java

1 Answer

0 votes
public class MyClass {
   
    private MyClass() {
        System.out.println("This is private constructor");
    }

    public static void AMethod() {
        MyClass obj = new MyClass();
    }
    
    public static void main(String args[]) {
        MyClass.AMethod();
    }
}




/*
run:

This is private constructor

*/

 



answered Jan 18, 2022 by avibootz
...