Hi, I’m very new to java and Codecademy, and for whatever reason I’m struggling on making a constructor , the code above is essentially the same as the code in the lessons, but it shows an error because the Person method doesn’t specify a return type, but i’m pretty sure that constructor methods don’t usually return things, and if if do try and make it return something, it won’t make objects. What am I doing wrong?
the name of the class (and the file) must match the name of the constructor.
If you want the class to be
naughtyAndNice then the constructor could be
public naughtyAndNice and the filename would be
Or, if you want the class to be
Person, then the constructor should be
public Person and the file name should be
For the constructor, you have
public void naughtyOrNice(boolean elf, boolean, detention, boolean chores)
but there should be no return type for the constructor (other than he class name)
void should not be there.
It should be
public naughtyOrNice(boolean elf, boolean, detention, boolean chores)
(otherwise Java tries to use a default constructor with no arguments when you’re creating an instance)
Thanks so much it’s working now, out of curiosity, do you know why they all had to have the same name?