-1

I usually do my homework on a Java file and submit the file to complete my task, so I separate each file and contain them all in one folder. Sometimes there are two works that have the same class name, but different work. If I let two files have the same class name, they won't run properly. (I'm using vscode)

For example:

File1.java

public class File1{
    public static void main(String[] args) {
            ...        
    }
}
class Fraction {...}

File2.java

public class File2{
    public static void main(String[] args) {
            ...        
    }
}
class Fraction {...} //this is not the same with File1.java

Can I do anything keep two class names the same in two different files in one folder?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Pson
  • 19
  • 3
  • 1
    'Sometimes there are 2 works that have the same class name': that's the problem. Solution: don't. You can't rationally expect to have two files with the same name. – user207421 Aug 30 '22 at 09:29
  • 1
    When you just run them (JDK 11 or newer), using `java File1.java` or `java File2.java`, no class files will be stored on disc and there’s no clash. However, when you submit your homework, you’re dependent on how your teacher will run the code. – Holger Sep 08 '22 at 15:38

2 Answers2

-2

Do not define the same class name (whether the same script or not) in the same folder. This throws an error many times.

JialeDu
  • 6,021
  • 2
  • 5
  • 24
-2

Largely all filenames are Operating System rules dependent. Java source code files must be named by the main class name of the code in the file. After compiling, a file of the same name but different extension, extension of .class will be there. Dependent how the compiler command line flags are set it will simply by default overwrite any old version .class file. type javac -h on the shell or prompt for compile command flags info.

Samuel Marchant
  • 331
  • 2
  • 6