Question

I'm just beginning an assignment where I have to use multiple classes in a program. My textbook shows a main method at the top of the source code, with a new class called SimpleCircle at the bottom. I tried formatting my source code the same way, but I just get 'error: missing method body, or declare abstract'. My code looks like this:

public class AssignmentTen
{
    public static void main (String[] args)
    {   
        Employee e1 = new Employee();
        System.out.println(e1.number);
    }
}

class Employee
{
    int number;

    Employee();
    {
        number = 1;
    }
}

Is it possible for me to keep the Employee class in my source code? If so, what should I change?

Was it helpful?

Solution

It should be Employee() rather than Employee();.

class Employee
{
    int number;

    Employee()
    {
        number = 1;
    }
}
  • You can have multiple classes in the same source file
  • But only one class can be public
  • The name of file should be same as of public class

OTHER TIPS

The error happens because you have a semi-colon in this line that should not be there:

Employee();

Remove the semi-colon.

You can have multiple classes in a source file, but only one of the classes can be public and the filename of the file must be the same as the public class.

Yes, you can include other classes along with your main class in the same file. The only requirement is that at most one class there is public, and that the name of that public class matches the name of the file.

As far as your example goes, you need to remove the semicolon between the list of parameters and the body of the constructor. Use an IDE, it should point the problem to you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top