0

I'm a beginning computer science student going through the Java gauntlet. We had to write a program with an error in it; it was a fun exercise actually. One of my classmates emailed me a patch(in English not diff) and said that he add public to the class.

This made me wonder. Does it matter or is it just a convention to declare the class public. The program worked and from what I understand the default is package-private which I think would be best for crappy little scripts that CS students are mailing around to each other.

Can anybody give me some more insight into this—in regards to Java mostly but general CS theory might prove insightful as well—and maybe some terms that I can use to research these concepts further.

I want to know if my classmates correction is valid and or important and why.

EDIT
sorry here's my original program without an error

import java.util.Scanner;
/**
 * Universal hello translator
 * Author: Noel Niles mail: noelniles@gmail.com
 *
 */
class HelloUniverse {
    public static void main (String [] args)
    {
        /* code */
        int country; //number of country from list
        String name; //name of person
        String greeting; //hello in different languages

        Scanner key = new Scanner(System.in);
        System.out.print("Where are you from?\n"+
                         "Enter a number from the list:\n");

        //TODO(anyone): Add some more countries and greetings
        System.out.print("1. Afganistan\n" +
                         "2. Antarctica\n" +
                         "3. Australia\n"  +
                         "4. Austria\n"    +
                         "5. Bangladesh\n" +
                         "6. Belgium\n"    +
                         "7. Brazil\n"     +
                         "8. Burma\n"      +
                         "9. Canada\n"     +
                         "10. Chile\n"     +
                         "11. China\n"     );

        //get the country code
        country = key.nextInt();

        key.nextLine();
        //get the users name
        System.out.println("What is you name?");
        name = key.nextLine();

        switch (country) { 
            case 1: greeting = "salaam";
                    break;
            case 2: greeting = "h-h-h-i-i thththththere";           
                    break;
            case 3: greeting = "G'day mate";
                    break;
            case 4: greeting = "Gruss Gott";
                    break;
            case 5: greeting = "nomoskar";
                    break;
            case 6: greeting = "Hallo";
                    break;
            case 7: greeting = "Ola";
                    break;
            case 8: greeting = "mingalaba";
                    break;
            case 9: greeting = "Good day eh";
                    break;
            case 10: greeting = "Hola";
                     break;
            case 11: greeting = "Nei hou";
                     break;
            default: greeting = "Invalid country";
                     break;
        }

        //display the greeting and the users name
        System.out.printf("%s %s\n", greeting, name);
    }
}
noel
  • 2,257
  • 3
  • 24
  • 39
  • I don't get it. If the class defines itself as a crappy script, and your only goal is to make it work, and it works as is, why do you care about its visibility modifier? – JB Nizet Apr 11 '13 at 07:32
  • Huh? I think we mean the same thing. It just kind of saves key strokes. but I wanted to know if there was some convention I was breaking. – noel Apr 11 '13 at 07:35
  • I would make a main class public, mainly for this class to appear in the generated javadoc, and thus be able to know that it's a main class, and the accepted arguments of the main method. – JB Nizet Apr 11 '13 at 07:37

2 Answers2

3

No, it's fine for the class containing the main method to be non-public. (It can even be a private nested class if you really want.)

The main method itself has to be public, but the class doesn't.

(Note that there's no "general CS theory" here - each language and platform has its own conventions and rules here.)

(I would strongly suggest using an array of greetings instead of a giant switch statement, admittedly... but that's a different matter.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Cool thanks I just posted my code in case you wanna be bored :) – noel Apr 11 '13 at 07:27
  • Is there any reason not to declare it public? – noel Apr 11 '13 at 07:33
  • @shakabra: Different people take different approaches. I like to keep things as private as they can be, on the grounds that it gives more room for implementation change later. For a single throwaway program, it makes no difference whatsoever. – Jon Skeet Apr 11 '13 at 09:49
0

When your class does not have any modifier, it means that this class will still be visible in the same package or in your class. See the different access levels here : http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

willome
  • 3,062
  • 19
  • 32