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?

Ken
  • 343
  • 7
  • 20
  • 1
    The methods have to go inside the classes. That means they have to go inside the `{` and `}` that belong to each class. – ajb Sep 29 '14 at 21:37
  • possible duplicate of [Multiple classes in single file](http://stackoverflow.com/questions/13132729/multiple-classes-in-single-file) – KevinDTimm Sep 29 '14 at 21:46

3 Answers3

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 {}
}
M A
  • 71,713
  • 13
  • 134
  • 174
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

KevinDTimm
  • 14,226
  • 3
  • 42
  • 60
  • 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. – Ken Sep 29 '14 at 21:42
  • 1
    It's too bad that instructors do that, it's considered to be bad form (which your prof should know) – KevinDTimm Sep 29 '14 at 21:46
-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.

austin wernli
  • 1,801
  • 12
  • 15
  • http://stackoverflow.com/questions/2336692/java-multiple-class-declarations-in-one-file... You can have multiple classes but at most one public class – austin wernli Sep 29 '14 at 21:45