Search code examples
javavisual-studio-codeconstructorjavac

Why does this Java code using multiple constructors run in VS code but won't compile with javac?


I've been experimenting with using multiple constructors in Java. Below is my code:

public class MultipleConstructors {
    int x = 20;
    int y = 50;
    String color = "Green";
    String color2 = "Yellow";

    public MultipleConstructors() {

    }

    public MultipleConstructors(int numb, int numb2, String colOne, String colTwo) {
        x = numb;
        y = numb2;
        color = colOne;
        color2 = colTwo;
    }

    public MultipleConstructors(int numb3, int numb4, String colThree, String colFour) {
        x = numb3;
        y = numb4; 
        color = colThree;
        color2 = colFour;
    }

    public MultipleConstructors(int numb5, int numb6, String colFive, String colSix) {
        x = numb5;
        y = numb6; 
        color = colFive;
        color2 = colSix;
    }

    public static void main(String[] args) {
        MultipleConstructors myObjOne = new MultipleConstructors(100, 200, "Pink", "Blue");
        MultipleConstructors myObjTwo = new MultipleConstructors(300, 500, "Burgandy", "Silver");
        MultipleConstructors myObjThree = new MultipleConstructors(800, 1000, "Black", "White");
        MultipleConstructors myObjFour = new MultipleConstructors();

        System.out.println(myObjOne.x + myObjOne.y + " " + myObjOne.color + " " + myObjTwo.color2 + " " + " : SUCCESS");

        System.out.println(myObjTwo.x + myObjTwo.y + " " + myObjTwo.color + " " + myObjTwo.color2 + " " + " : VICTORY");

        System.out.println(myObjThree.x + myObjThree.y + " " + myObjThree.color + " " + myObjThree.color2 + " " + " : YES");

        System.out.println(myObjFour.x + myObjFour.y + " " + myObjFour.color + " " + myObjFour.color2 + " " + " : YES");
    }
}

It runs in VS Code but will not compile using javac command. I think it might be the undefined constructor public MultipleConstructors() {} after the parent attribute declarations.


Solution

  • If you get something like the following error message:

    MultipleConstructors.java:11: error: constructor MultipleConstructors(int,int,java.lang.String,java.lang.String) is already defined in class MultipleConstructors
    

    Then you should use this answer. If you are having a ClassNotFoundException, you should see Ntshembo Hlongwane's answer.

    Your issue here is that you have multiple of the same constructor. I think what you are going for is constructor overloading, which means you can have multiple constructors with different headers, but three of your constructors have the same header. See my comments inline with your constructors below:

    public MultipleConstructors() {
        // header has no parameters
        // fine, leaves values alone
    }
    
    public MultipleConstructors(int numb, int numb2, String colOne, String colTwo) {
        // header has parameters int, int String String
        //fine, offers you the opportunity to change those values
    
        x = numb;
        y = numb2;
        color = colOne;
        color2 = colTwo;
    }
    
    public MultipleConstructors(int numb3, int numb4, String colThree, String colFour) {
        // header has parameters int, int, String, String
        // Wait, that's the same as the one above!
    
        x = numb3;
        y = numb4; 
        color = colThree;
        color2 = colFour;
    }
    
    public MultipleConstructors(int numb5, int numb6, String colFive, String colSix) {
        // header has parameters int, int, String, String
        // Wait, that's also the same as the one above!
    
        x = numb5;
        y = numb6; 
        color = colFive;
        color2 = colSix;
    }
    

    When you instantiate this object, and you give it two ints and a string, which constructor do you expect to be called? This is a logic error, not a syntax error or a compiler error, because your code is ambiguous. Hope this helps, and good luck in your learning journey. :)