0

I am new to Java, coming mainly from C++ & JS, and I am just experimenting trying to learn the differences and what not and I came across something unusual. I am trying to pass an array of doubles into a constructor method, but the way I was trying would not work. The method signature for the ctor is

public Student(int sid, String name, double [] grades)

The way I was trying to pass the array was

Student s1 = new Student(1, "Mike", [99.9,88.8]);

however the only way i could get it to work was by creating an array double[] grade = {99.9,88.9} and passing that to the ctor like Student s1 = new Student(1, "Mike", grade)

So my question is, is it possible to do it the way i first tried Student s1 = new Student(1, "Mike", [99.9,88.8]); or something similar?

Thank you.

@ernest_k did you even read my question before closing it? the thread you linked was a completely different issue.

d0rf47
  • 189
  • 9
  • 1
    `new Student(1, "Mike", new double[] { 99.9, 88.8 })` – Andreas Jan 18 '20 at 01:44
  • Oh i didnt realize i had to use the new specifier there. Thank you for the answer. – d0rf47 Jan 18 '20 at 01:47
  • @TomHawtin-tackline It wont work when i do that that's what i dont understand. Cause my ctor looks like that – d0rf47 Jan 18 '20 at 01:53
  • 2
    @d0rf47 I completely failed to modify the constructor! Let me try again: If you modified the constructor to `public Student(int sid, String name, double... grades)` it would still work with array but you could write `new Student(1, "Mike", 99.9, 88.8)`. – Tom Hawtin - tackline Jan 18 '20 at 02:03
  • 1
    @TomHawtin-tackline wow thats amazing! thank you so much! that is exactly what i was looking for! – d0rf47 Jan 18 '20 at 02:04

0 Answers0