1

So for this assignment I have to use 3 classes in one file, and each of these classes has about 2 methods. I'm not quite sure how I should construct this.

I was simply thinking about this (this is just a "sketch"):

public class{
main{}
}
method 1 {}
method 2 {}

class 2{}
method 3{}
method 4{}

class 3{}
method 5{}
method 6{}

Is this the correct structure? Or should I put the 3 classes together first and then all the methods on together on the bottom?

2
  • 1
    The methods have to go inside the classes. That means they have to go inside the { and } that belong to each class. Commented Sep 29, 2014 at 21:37
  • possible duplicate of Multiple classes in single file Commented Sep 29, 2014 at 21:46

3 Answers 3

2

You can't place methods outside of class definitions. Every method must belong to a class. And you can only have one public class which must have the same name as the Java file. Following your sketch:

public class C1 {
   main{}

   method1 {}
   method2 {}
}


class C2 {
   method3 {}
   method4 {}
}

class C3 {
   method5 {}
   method6 {}
}
Sign up to request clarification or add additional context in comments.

Comments

1

1) Each class should be placed into a file by itself (unless you don't want to reference the class outside of that file)
2) Java is not C++; classes encompass their methods

2 Comments

In this case I can't put 2 classes in 2 different files as I can only upload one file to the online automatic grader. I thought about that.
It's too bad that instructors do that, it's considered to be bad form (which your prof should know)
-1
    class one
{
    public void method1
    {

    }

    public void method2
    {

    }
}

class two
{
    public void method1
    {

    }

    public void method2
    {

    }
}

class three
{
    public void method1
    {

    }

    public void method2
    {

    }
}

Sorry, it's in c# so the brackets are off from java standards. But yeah, the methods have to be inside of the classes.

1 Comment

stackoverflow.com/questions/2336692/…... You can have multiple classes but at most one public class

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.