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:
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
CodePudding user response:
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);
}
}
CodePudding user response:
You should change the name of your class, since Java already has a class name Object or add a constructor for your Class. Use for example the name MyObject.java