How to initialize an object with an array parameter in Java

2.8k views Asked by At

I am trying to write a code where the object of a class has an array in it's parameter. I have written the parameterized constructor like as follows -

public abstract class StudentHS {

private String firstName;
private String lastName;
private String rollNumber;
public int[] marksAcquired;
public int hundreds;

// constructor
public StudentHS (String first, String last, String roll, int[] marks) {

    firstName = first;
    lastName = last;
    rollNumber = roll;
    marksAcquired = marks;

}

Now, when I am trying to initialize an object of this class in an array of objects, I am getting an error that says that the constructor is undefined.

public class ResultSystemTest {

public static void main(String[] args) {
    StudentHS studentArts[] = new StudentHS[3];
    StudentHS studentCommerce[] = new StudentHS[3];
    StudentHS studentScience[] = new StudentHS[3];

    studentArts[ 0 ] = new StudentArts("Priyanka", "Ray", "01", {56, 59, 61, 72, 65, 63, 58});
}

StudentArts, StudentCommerce and StudentScience are subclasses of StudentHS here.

Where am I going wrong here?

1

There are 1 answers

0
singhakash On BEST ANSWER

You have to define its type while passing like

 studentArts[ 0 ] = new StudentArts("Priyanka", "Ray", "01", new int[]{56, 59, 61, 72, 65, 63, 58});