Jump to content
  • Advertisement
Sign in to follow this  
destructivArts

Java code error

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!