0
Class myclass is public, should be declared in a file named Myclass.java error
I am using the blue J IDE to practice imputing code and when imputing exactly what I see in sololearn I run into this error message when compiling the code
8 Answers
+ 2
your class needs to have the same name as the file so MyClass is in MyClass.java
+ 1
you are almost done what you need to do is to cut out the code of the second Myclass (rename it to Person) and put it in its own file Person.java
https://code.sololearn.com/cByKshZspsip/#java
+ 1
"Error ';' expected" says that somewhere in your code is missing
a ";" at the end.
the sololearn playground can only use 1 file but it seems to seperate it internal.
0
Try to name your class Myclass.
Usually classes are always using "CamelCase" E.g. MyClass.
0
I named the new project file person.Java and the class MyClass yet I am still running into the issue of it telling me that "class person is public, should be declared in a file named person.Java which I believe I have done not quite sure where I'm going wrong
0
ahh gottcha I think I'm getting somewhere now however it now tells me I have a duplicate class which I'm not sure how to address here is my input into blue j in a file named Myclass.java
public class Myclass {
public static void main(String[ ] args) {
Person j;
j = new Person("John");
j.setAge(20);
celebrateBirthday(j);
System.out.println(j.getAge());
}
static void celebrateBirthday(Person p) {
p.setAge(p.getAge() + 1);
}
}
public class Myclass {
private String name;
private int age;
Myclass (String n) {
this.name = n;
}
public int getAge() {
return age;
}
public void setAge(int a) {
this.age = a;
}
}
the duplicate error highlights the second instance of "Myclass" and when changed to Person returns what I mentioned in my previous post. do I need to create another class/object titled Person?
0
so two different files one with the top lines and one with the bottom? its now asking me to input something when creating the object. Instance is person1 and when I try to enter String n within the parentheses and it says "Error ';' expected". does sololearns IDE automatically compile both as separate files and outputs the result?
is that why its a little more complex on blue j?
0
got it I think I figures things out the classes got be separate and then you call the output via one of them