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);
}
}
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.)
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.