• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

public class problem

 
vaibhav panghal
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ,
if i make a file called B.java that contains a class like :-
class A
{
..
..
}
then if i compile the file it compiles fine and produces A.class file .
but if i make a file called B.java that contains a class like :-
pulic class A
{
..
..
}
then there is a compilation error and the compiler tells me to match the names of my file and my pulic class .
Can anyone tell me what's going on here ? am i doing something wrong ?
 
Seamus Minogue
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you want your class and file names to be different?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49793
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you search you can find this, which probably gives more information about this question. It comes up frequently!
 
sudheer kiran
Ranch Hand
Posts: 237
Firefox Browser MyEclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
///////////////////////////////////
class Hello
{
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}
///////////////////////
save the above code in Hi.java file
compile it using : javac Hi.java
can run it using : java Hello.java
 
Sidharth Pallai
Ranch Hand
Posts: 134
Hibernate Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its the issue with the access modifier you use to declare a class.If you declare a class public, then you need to save the file with the name of the class.With a default access modifier you can save with any filename, but you need to use the actual class name while running your program and filename to compile it.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49793
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch, sudheer kancherla.

The rule (which is imposed by most compilers) is that the name of the file must be the same as the name of a top-level public class in it, with the .java extension. This rule does not apply to any classes not declared public.
 
vaibhav panghal
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Sidharth . hi Campbell .
all right i got some idea about it as Campbell said that its sort of a rule that's forced by the java compiler . but what i want to know is - why such a rule ?? why didn't we have such a rule in C++ ?? why - that's my real question
 
Campbell Ritchie
Sheriff
Pie
Posts: 49793
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Beware of asking "why *** in Java when %%% in C++." Java and C++ are different languages and they work differently and people get very confused thinking "protected" or "static" mean the same in the two languages.

I know it has something to do with allowing better access to the code for the javac tool. Find my earlier post on this thread and find the link to the post I quoted and read what Ernest Friedmann-Hill wrote there. See whether that explains it, far better than I would!

Another copy of the link here.
 
vaibhav panghal
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
all right i'll find it out myself and let you know .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic