Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Java error: Cannot be applied to given types

So I’m quite new to Java and this is the first time I’m working with objects.
Could you help me out with why this piece of code doesn’t work?

public class Object
{
    String a1;
    String[] a2;
    int a3;
    double a4;
    long a5;
}

And here is the main class:

public class Main
{
    public static void main(String[] args)
    {
        Object obj1 = new Object("example text", new String[] {"some", "more", "examples", "here"}, 1, 1.0);
    }
}

Error message:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

java: constructor Object in class Object cannot be applied to given types;
required: no arguments
found: java.lang.String,java.lang.String[],int,double
reason: actual and formal argument lists differ in length

>Solution :

You must declare a constructor for your Object class inside it as:

public class Object {
    String a1;
    String[] a2;
    int a3;
    double a4;
    long a5;

    public Object(String example_text, String[] strings, int i, double v) {
    }
}

And another important thing is that Object is a predefined class in Java, so you should use full package name of your own Object class in main method:

public class Main
{
    public static void main(String[] args)
    {
        Object obj1 = new path.to.Object("example text", new String[] {"some", "more", "examples", "here"}, 1, 1.0);
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading