Java Classes | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Java Classes

I did add keyword final but still having issues public class Person{ public static final String DEFAULT_NAME = ""; public static final int DEFAULT_AGE = 0; public String name; public int age; public Person(String name, int age){ this.name = name; this.age = age; } public String getName(){ return name; } public void setName(String name){ this.name = name; } public int getAge(){ return age; } public void setAge(int age){ this.age = age; } }

9th Apr 2019, 6:53 PM
Albert Yakubov
Albert Yakubov - avatar
4 Answers
+ 8
Looks ok to me is it because your missing the main method?
9th Apr 2019, 7:21 PM
D_Stark
D_Stark - avatar
+ 6
Albert Yakubov yes, your code works but its just abit muddled up which is acceptable when your a beginner, You have getters and setters which are used for encapsulation so your variables name and age should be private and you want to access these through methods and not directly which you can still do here. You need a main method as this is what java looks for to begin executing your code Also what do you want do with your final fields as there not doing much in this program at the moment 👍
9th Apr 2019, 7:34 PM
D_Stark
D_Stark - avatar
0
should i put anything in my main method
9th Apr 2019, 7:22 PM
Albert Yakubov
Albert Yakubov - avatar
0
public Person(){ was in the wrong spot. here is the right code to compare: public class Person{ public static final String DEFAULT_NAME = ""; public static final int DEFAULT_AGE = 0; public String name; public int age; public Person(String name, int age){ this.name = name; this.age = age; } public Person(){ this.name = DEFAULT_NAME; this.age = DEFAULT_AGE; } public String getName(){ return name; } public void setName(String name){ this.name = name; } public int getAge(){ return age; } public void setAge(int age){ this.age = age; } }
9th Apr 2019, 7:33 PM
Albert Yakubov
Albert Yakubov - avatar