Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

java  RSS feed

 
vignesh gopalakrishnan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.io.*;

public class locc1
{
int a;
public void get()
{
System.out.println(a);
}
}


class locsam1
{
public static void main(String args[])
{
locc1 l=new locc1();
l.a=98;
l.get();
}
}


hai guys .. upon execution of this program it shows me an error as " class locc1 is public, should be declared in a file named locc1.java .. why do error occurs .? and i want to know the solution to get rid of this error and also i want the reason why this error is raised . please guys help me.. thanks in advance .
 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answers to your question are all right there in the error message.

The rule is that a public class named X must be in a file named X.java -- in your case, the public class named locc1 must be in a file named locc1.java. How should you get rid of that error? Well, why don't you come up with some possible solutions and we can discuss them if they don't seem obvious to you?
 
vignesh gopalakrishnan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:The answers to your question are all right there in the error message.

The rule is that a public class named X must be in a file named X.java -- in your case, the public class named locc1 must be in a file named locc1.java. How should you get rid of that error? Well, why don't you come up with some possible solutions and we can discuss them if they don't seem obvious to you?


. the solution is nothing but , simply removing the public access specifier that is infront of the class declaration . program successfully executes . but why does that happens is my question ..?? i mean what is the exact reason behind this error . ??
 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean, the "exact" reason? I already told you what the rule is. That's the reason for the error message.
 
vignesh gopalakrishnan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:What do you mean, the "exact" reason? I already told you what the rule is. That's the reason for the error message.



i just forgotten to say thanks for your response , paul clapham . but, am extremely sorry , i think , i have to make my question clear .
this is my question

does , simply putting public infront of a class means anything to the JVM or to the compiler ..?? because the compiler initially checks for the class where main function is reposited . in this case locc1 is a separate class , then why does this error comes up..?? i mean the compiler should only check for the class named locsam1 . isn't it..?? you are saying that it is by RULE . and yeah i accept that , what does the rule do in this case .?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read this again:

class locc1 is public, should be declared in a file named locc1.java


the CLASS is public. The rules for the compiler say that any public class MUST be in a file with the same name. Why? Because the designers said so. Why did they do that? You'd have to ask them to be sure, but the theory I've heard most often is that it makes it easier for the compiler to find the file that needs to be compiled.

This really has nothing to do with the main method. You can create a class that does not have a main method, and if the class is public, the file name must still match.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!