# Java code error

This topic is 2460 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I've only been working with Java for a few days now. I'm using a book and some of the code is consistently not working,
When I try and define a class, the way the book says, I get an error.
Here's the code:
public class Weather { public static void main(String[] arguments) { // code } }

And heres the error:
weather.java:1: class Weather is public, should be declared in a file named Weather.java public class Weather { ^ 1 error 

I don't know enough to understand this, and the book hasn't explained it yet,
Could someone please explain what the error means, and how i would fix it?
Thank you,
Peter Slattery

##### Share on other sites
Hidden
Alright, I just changed the code so that in the class definition, its weather, not Weather, and it works fine,
Again could someone please explain why? I understand that Java is case sensitive, so does that mean that in the default class library (I know theres a name for it, just cant remember) that there is a class called Weather, and it thinks I'm calling it?

Thanks again,
Peter

No... It means that in Java, a class file must be named the same as the class. Meaning if your compiled class is called "weather.java" then your class must be called "weather", but if your file is called "Weather.java" then your class must be named "Weather".

Note: I believe this is only true when your class is public.

##### Share on other sites
This has nothing todo with OP question, but anyway ...

Note: I believe this is only true when your class is public.

Only the outer class must have a matching name, regardless of the qualifier used.

You can include inner classes, that may have different names.

For example; this is valid.

 // Weather.java public class Weather { // Weather code ... public class Sunny { } } 

 // SomeOtherClass.java import Weather$Sunny; class SomeOtherClass { public SomeOtherClass() { Weather$Sunny sunny = new Weather\$Sunny(); } } 

##### Share on other sites
Wow, thats a lot simpler than I was thinking, thank you both