JAVA

[ 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 wiki.java
java code
public class techy 
{     
        public static void main(String[] args) 
{
            System.out.println("Hello, wikitechy!");
        }
    }	

1 error found:

File: wiki.java  [line: 1]

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

  • 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 “wiki.java”
  • As the error message suggests, if we declare a class as public, it needs its wiki.java file. If we don’t want to do that, we don’t define it as public.
READ  Java Programming - Longest Common Subsequence

About the author

Venkatesan Prabu

Venkatesan Prabu

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