This is some information on the Java 6 certification that I prepare. This information is not very well structured and it is most of the time an answer to the questions that come when I read the SCJP 6 book (Bates and Sierra). *** AS OF 5 OF MARCH 2010, I AM SCJP 6.0 ***

Thursday, January 28, 2010

25 - Review - private - level basic - Java 1.6.0_17 - NB 6.8

I wanted to make you aware (if you are not) of the following...

If you make your constructor "private" you can't invoke the constructor from another class... But you can still invoke it from the SAME class. In Netbeans we are used that Netbeans will generate automatically a Main class and a pvsm (public static void main[] args) method in the main class. In this case if you try to instantiate a class using a private constructor you cant... You have to give another ways (have a look to singletons). But if you put a pvsm in the class were the private constructor is defined, no problem you can instantiate the class.

package pkg3.pkg31;

class Class1 {

    protected int a;
    protected int b;

    private Class1(int a, int b) {
        this.a = a;
        this.b = b;

    protected int calculation() {
        return a + b;

    public static void main(String[] args) {
        System.out.println(new Class1(1, 2).calculation());

BUILD SUCCESSFUL (total time: 0 seconds)

No comments:

Post a Comment