0

I am practicing with Java, and I'm trying to build an constructor that takes in an int[] array, and an int.

public static App(int[] name, int n) {}

Now in my main(), I am trying to build a new object with the constructor, and I'm getting an error message.

public static void main(String[] args) {
    int n;
    int[] name = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

    App app1 = new App(name, n);

}

The error message I'm getting back is:

"constructor App in class App cannot be applied to given types; required: no arguments; found: int[],int; reason: actual and formal argument lists differ in length"

Can someone please explain more about what this error means, and how can I correct my code?

3
  • 5
    A constructor can't have a static modifier. Commented Apr 12, 2017 at 16:32
  • 1
    A constructor should (a) have the exact same name as the class (including upper/lower case), (b) no return type, (c) no static modifier. Commented Apr 12, 2017 at 16:38
  • 1
    a constructor cannot have a return type and must not be static also you dont want it to be private Commented Apr 12, 2017 at 16:43

3 Answers 3

1
public static App(int[] name, int n) {}

Thae became a static method with invalid signature and that is not constructor.

According to your description, It should be

public App(int[] name, int n) {

}
Sign up to request clarification or add additional context in comments.

5 Comments

I have changed from public static to public on the constructor, and I am still getting the same error message. To be more clear, my constructor now reads public int[] app(int[] name, int n) {}
@as.beaulieu It should not be. Atleast a different error message. Can you post a screenshot ?
@as.beaulieu Why there is a return again ? it should be just public App(int[] name, int n) {}
@as.beaulieu, what is app? Isn't your class named App? Please clarify.
@as.beaulieu it must not have a return type not even void please change your constructor to the specified answers
1

A constructor does not have a static modifier, and a static method needs a return type. Your posted code isn't valid.

public static App(int[] name, int n) {}

should be

public App(int[] name, int n) {}

As it is, your class doesn't have a constructor at all - so you get the default (no-args) constructor.

Comments

1

There are two problems with your code, you need to resolve both of them to make it compiled.

(1) A constructor can't have static keyword, so remove it

(2) variable n is not initialized, so initialize it before its first use as shown below:

int n=1; //or any value you wanted

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.