Jump to content

  • Log In with Google      Sign In   
  • Create Account


Java code error


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 destructivArts   Members   -  Reputation: 205

Like
0Likes
Like

Posted 28 April 2011 - 02:14 PM

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
-------------------------------------
"Other than that, I have no opinion."
My Blog - Check it Out

Sponsor:

#2 Zael   Members   -  Reputation: 154

Like
1Likes
Like

Posted 28 April 2011 - 02:53 PM

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.

#3 Angex   Members   -  Reputation: 837

Like
0Likes
Like

Posted 29 April 2011 - 04:58 AM

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();
	}

}



#4 destructivArts   Members   -  Reputation: 205

Like
0Likes
Like

Posted 29 April 2011 - 01:46 PM

Wow, thats a lot simpler than I was thinking, thank you both
-------------------------------------
"Other than that, I have no opinion."
My Blog - Check it Out




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS