This week's book giveaway is in the Cloud forum.
We're giving away four copies of Terraform in Action and have Scott Winkler on-line!
See this thread for details.
Win a copy of Terraform in Action this week in the Cloud forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Need clarity on .class/.java naming logic

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Saloon Keeper
Posts: 7181
166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You would get a compilation error if the class was declared "public", as in "public class Naming".
 
Brendon McFadden
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Tim Moores
Saloon Keeper
Posts: 7181
166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. The main method has in any case a special position, as it's used to run code the command line where there is not (yet) an "object environment".
 
Marshal
Posts: 74392
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The rule about being public and being in a file with the same name only applies to top-level classes.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic