0

Just noticed when I was creating model I'm able to create a private constructor... Probably java or eclipse bug just need to confirm. I know it's kinda impossible, but try yourself.

Create an any model ie.

//case1
public class Main {

private Main(){}
public Main(Double...doubles ){}
}

Normally you couldn't create an object Main m = new Main(); but when u create an model like that :

//case2
public class Main {

private Main(){}
public Main(Double [] doubles ){}
}

You are unable to create and object Main m = new Main();.

Is it how its suppose to be? Will be grateful for any explanation.

Java :build 1.8.0_131-b11

Eclipse : Version: Neon.3 Release (4.6.3)

Michael
  • 71
  • 2
  • 10
  • Possible duplicate of [Can a constructor in Java be private?](http://stackoverflow.com/questions/2816123/can-a-constructor-in-java-be-private) – Harkamwaljeet Singh May 03 '17 at 22:09
  • "Probably java [sic] or eclipse [sic] bug" - umm, yeah, you're never going to have that be the case. Might as well remove that conjecture from your debugging toolkit permanently. That's just wishful magical thinking. – Lew Bloch May 04 '17 at 10:31

3 Answers3

3

That's not a bug. In case 2, you are actually creating a private constructor which means you cannot instantiate an object of Main using the default constructor that takes no args. You can only construct an object using the private constructor in the same class where it is defined.

public class Main {

     private Main() {
     }

     public Main(Double[] doubles) {
     }

     public void testPrivateConstructor() {
         Main mainObjectInsideTheSameClass = new Main();
     }
 }

Your second constructor in case 1 is public and it takes variable number of arguments. So when you create an object using Main m = new Main() you are really using the second public constructor and you are passing zero arguments.

One of the specific use cases for case 2 is singleton pattern in Java.
Refer: Can a constructor in Java be private?

Community
  • 1
  • 1
qrius
  • 621
  • 2
  • 9
  • 22
  • i can that's all about... try urself – Michael May 03 '17 at 22:17
  • You cannot create object by `Main m = new Main();`. You would be using the other constructor and passing in a `Double` array.. – qrius May 03 '17 at 22:18
  • did you try? Create an model like i've just write and another class with `public static void main(String[] args) {` and try yourself... maybe its just my eclipse ... – Michael May 03 '17 at 22:24
  • In your question, you are saying "You are unable to create and object Main m = new Main();" which is exactly how it's supposed to be. So what are you trying to say? – qrius May 03 '17 at 22:26
  • Why i'm able to create an object in `case 1` and unable to create an object in `case 2`? – Michael May 03 '17 at 22:30
  • 1
    Because your second constructor in case 1 is public and it takes variable number of arguments. So when you create an object using `Main m = new Main()` you are really using the second public constructor and you are passing *zero* arguments. – qrius May 03 '17 at 22:32
0

That's how its supposed to be. You are allowed to create private constructors in Java . Please see this link

Community
  • 1
  • 1
0

You can use private constructors in any of that class methods (even if that method is static).

Creation of testA use TestA(Double... d) constructor. Double... it is varargs which allows pass zero or multiple arguments.

public class Main
{
    private Main()
    {
        System.out.println("Main1");
    }

    public Main(Double[] d)
    {
        System.out.println("Main1 " + (Arrays.toString(d)));
    }

    public static void main(String[] args)
    {
        TestA testA = new TestA(); //match to TestA(Double... d) with zero args
//      TestB testB = new TestB(); //ERROR don't match to TestB(Double[] d)
        Main main = new Main(); //you can use private constructors in class
    }
}

class TestA
{
    private TestA()
    {
        System.out.println("TestA1");
    }

    public TestA(Double... d)
    {
        System.out.println("TestB2 " + (Arrays.toString(d)));
    }
}

class TestB
{
    private TestB()
    {
        System.out.println("TestB1");
    }

    public TestB(Double[] d)
    {
        System.out.println("TestB2 " + (Arrays.toString(d)));
    }
}

Out:

TestA2 []
Main1
obywatelgcc
  • 93
  • 2
  • 6