[ Solution 1 Answers ] Class Is Public, Should Be Declared In A File Named .Java

  • When the class name and the filename of a given Java program doesn’t match then we will get this error.
  • Considering an example where the file is named as
java code
public class techy 
        public static void main(String[] args) 
            System.out.println("Hello, wikitechy!");

1 error found:

File:  [line: 1]

Error: class techy is public, should be declared in a file named

  • Since wiki does not match with techy, the code will not compile properly. To fix this error kind of error, either we need to rename the file or change the class name as “”
  • As the error message suggests, if we declare a class as public, it needs its file. If we don’t want to do that, we don’t define it as public.
See also  [ Solved -8 Answers ] JAVA - Java function for arrays like PHP's join()?

About the author

Wikitechy Editor

Wikitechy Founder, Author, International Speaker, and Job Consultant. My role as the CEO of Wikitechy, I help businesses build their next generation digital platforms and help with their product innovation and growth strategy. I'm a frequent speaker at tech conferences and events.

Add Comment

Click here to post a comment