aspose file tools*
The moose likes Beginning Java and the fly likes public class problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "public class problem" Watch "public class problem" New topic
Author

public class problem

vaibhav panghal
Greenhorn

Joined: Feb 24, 2008
Posts: 25
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

Joined: Jun 24, 2008
Posts: 41
Why do you want your class and file names to be different?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39884
    
  28
If you search you can find this, which probably gives more information about this question. It comes up frequently!
sudheer kiran
Ranch Hand

Joined: Jun 26, 2008
Posts: 237

///////////////////////////////////
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


Sudheer
SCWCD, SCJP 5
Sidharth Pallai
Ranch Hand

Joined: Apr 21, 2008
Posts: 134

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.


Thanks & Regards
Sidharth Pallai
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39884
    
  28
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

Joined: Feb 24, 2008
Posts: 25
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

Joined: Oct 13, 2005
Posts: 39884
    
  28
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

Joined: Feb 24, 2008
Posts: 25
all right i'll find it out myself and let you know .
 
 
subject: public class problem