Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!

Brendon McFadden

Greenhorn
+ Follow
since Oct 31, 2011
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Brendon McFadden

Tim Moores wrote:You would get a compilation error if the class was declared "public", as in "public class Naming".



Ahh I see, thank you. So declaring a class and main method as public are two very different things?
10 years ago
Hi there,

I'm a very new starter to java (obviously) and I'd like someone to explain why I was able to create a source file (.java) with a different name to the class and still have it work? Through all the info I could find it shouldn't as it's a public file.

I named my source file Another.java and it looked like:

class Naming

{
public static void main (String[] args)
{
System.out.println("This means it worked... again!");
}
}

I then ran "javac Another.java" which worked, and when running "java Naming" that printed off the text.

Have I done something wrong for this to still work? Or is it expected and just an organisational idea to have the same soruce/class name? From my searches I should get an error?

Thanks.
10 years ago