+ 1

Declaring a class in Java?

I wrote some code that works here on SL Playground; but when I put it in a different compiler, it says I need to connect the class to a file. How do I fix this issue? "compiler.java:2: error: class Program is public, should be declared in a file named Program.java" /*####THE CODE####*/ import java.util.Scanner public class Program { public static void main(String[] args) { Scanner vv = new Scanner (System.in); System.out.println("Yes?"); String n = (vv.nextLine()); System.out.print("Ah.. Hello there, "); System.out.print(n); System.out.println("!"); } } // Output: Yes? Ah.. Hello there, <name>!

14th Jun 2019, 1:35 PM
Carter Tomas
Carter Tomas - avatar
1 ответ
+ 4
File name = class name. If your class has the name program then your file name should be also program. https://stackoverflow.com/questions/13811020/error-class-x-is-public-should-be-declared-in-a-file-named-x-java
14th Jun 2019, 2:44 PM
Denise Roßberg
Denise Roßberg - avatar