1

I am a Java learner and while I was studying, I realized a book uses 2 or 3 classes in a same file. But when I actually followed the book's codes I was not able to run it.

for example,

Class Point {...}
Class Rectangle{...}
Class Triangle{...}
Class Main{...}

Are there any way to do it? or just books are listing them for easy to look?

Sorry about poor English, thank you.

Dheeraj Joshi
  • 1,522
  • 14
  • 23
  • 1
    Yes, you can. You should only keep classes that are closely linked in the same file really though. Depending on the purpose of the class, you have to make an `object` and then call the methods of the class via that. Check out [classes tutorial](https://www.youtube.com/watch?v=XqTg2buXS5o) –  Dec 04 '17 at 03:39
  • 2
    Only one can be public. Since you want to run the code, the one with the main routine should be the public one that matches the filename. – lockcmpxchg8b Dec 04 '17 at 03:42
  • 1
    Thank you so much guys! It helped a lot! – Philip Park Dec 04 '17 at 04:00
  • 1
    @PhilipPark happy coding –  Dec 04 '17 at 04:01

4 Answers4

2

Say you want to have two have 2 classes in one file, you can do this:

public class Main 
{
    public static void main(String[] args) 
    {    

        Human bob = new Human("bob");
        System.out.println(bob.name);
    }      

}

class Human
{
    public String name;
    public Human(String name)
    {
        this.name = name;
    }
}

If you put all this in one file, it would be named Main.java because the public class is called main.

  • Oh I get it. So Main class should be public and rest of classes are without public. I can run it now. Thank you so much!! – Philip Park Dec 04 '17 at 04:01
  • Plus, does matter public main place upper or lower? I mean order like Main, Human. or Human, Main? – Philip Park Dec 04 '17 at 04:05
  • 1
    Java is `case sensitive` Human isn't the same as writing human. Public, for `public class` has to be lowercase, as it is a `key word`. `Main` for `public class Main` doesn't have to be capitalized, but it should match the file name. If you called it main.java, `public class Main` wouldn't work it would have to be `public class main`. `Main` for the `public static void main` it is good practice to keep it lowercase. All methods should be written in [camel case](https://en.wikipedia.org/wiki/Camel_case) –  Dec 04 '17 at 04:08
1

While using multiple class in the same file without any public class in java

1.You will make a file with more then one class like

  class Game{
 public static void main(String[] args){
  System.out.print(""gaming");
  } 
  }

 class Movie{
 void main(){
 System.out.println("Movie");
}
}

2.Save the file with the name of class having the main method for above save it with Game.java

3.compile and run the file using the name Game

And the other one is with having a public class

1.suppose you have to classes in your file like

   public class Hello{
   public static void main(String[] args){
   System.out.print(""hello");
   } 
   }

  class Go{
  void main(){
  System.out.println("GO");
 }
 }

2.In java you can have only one public class in a single file so,you have to save the file with the name Hello.java

3.Now you can run and compile with Hello file name

Dheeraj Joshi
  • 1,522
  • 14
  • 23
0

-----Filename: Test.java-------------

class Test {

}
class Main{
    public static void main(String[] args) {
        System.out.println(new Main().toString());
    }
}
class Person{

}

class Other{

}
----------its legal--
niewj
  • 1
  • 2
-1
class Point {...}
class Rectangle{...}
class Triangle{...}
public class Main{...}

1. If you want to have more than one class in a file, you can dot it;

  1. but only have 1 public class in a file, and, the file name must be the same as the file of this public class.

  2. For example, the file name must be Main.java in this demo; This is no problem.

You can try agin.

niewj
  • 1
  • 2
  • Its no problem, do not have to be public, you can remove the identifier "public" too. The key is : if you want to declare a public class ,it must have the same name as the file name. – niewj Dec 04 '17 at 03:59