0

I've created a class called Student which will contain information about students.

public class Student {
    private int totalPoint;
    private int antalKurser;
    private String program;

// here is constructor and get/set methods
}

I'm also trying to create a ArrayList from that class and from what I understand i should write:

public class createArrayList {
        ArrayList<Student> studentList new ArrayList<>();
}

However Netbeans gives me an error and if I hover the line it says ';' expexted but I'm positive I have written that (above snippet is copy-pasted). When I run the code it says "compiled with errors" but still runs.

Found somewhere else I also could write:

ArrayList<Class> studentList = new ArrayList<>();

But then how does the application know which class it should create a list from?

The class Student is in another javafile but is in the same package. What am I missing?

8
  • 1
    Where's the =? You really need to learn the basic syntax. You can't just copy-paste code without understanding what you're doing. Commented Nov 15, 2017 at 20:13
  • ArrayList<Student> studentList = new ArrayList<>(); you need the = Commented Nov 15, 2017 at 20:13
  • In java 8 you don't need to add the class ArrayList<Student> studentList = new ArrayList<>(); If you use java 7 though you need to do ArrayList<Student> studentList = new ArrayList<Student>(); Commented Nov 15, 2017 at 20:16
  • Oh my, that was a silly mistake. Looked through the code thousands of times, still missed it :). Thanks! Commented Nov 15, 2017 at 20:16
  • 1
    @Axel P. sorry, wrong, Java 6 needed the class in generics creation :-) Commented Nov 15, 2017 at 20:18

1 Answer 1

1

Try

public class createArrayList {
        ArrayList<Student> studentList = new ArrayList<Student>();
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I missed the = even though I checked the code loads of times.

Your Answer

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