Java Reference
In-Depth Information
For now, you can think of public and static just as two keywords, which must be present to declare the main
method. Note that the JVM also requires that the name of the method must be main . This is the reason that I chose
main as the name of the method from the very beginning. It is just a rule imposed by the JVM to run a class that the
class must have a method named main whose declaration must look similar to as shown above. The final version of
the source code is shown in Listing 2-1. Save the source code in a file named Welcome.java .
Listing 2-1. Source Code for the Welcome Class
// Welcome.java
package com.jdojo.intro;
class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to the Java world.");
}
}
The Java compiler imposes restrictions on the file name of the source code. In this example, the name of the file
in which the source code for Welcome class is saved need not be Welcome.java . You could use another file name, for
example, MyWelcome.java , JWelcome.java , etc. If you declare a class as public, the source code for that class must be
saved in a file that has exactly the same name as the name of the class, plus the .java extension. You declare a class
public by using the public keyword before the class keyword in its declaration. The following snippet of code declares
the Welcome class public. For this purpose, it does not matter whether you declare the Welcome class public.
// Welcome class is public now
public class Welcome {
}
Every class in Java has two names
A simple name
The simple name of a class is the name that appears after the class keyword in the class declaration. In this
example, Welcome is the simple name of the class. The fully qualified name of a class is its package name followed by a
dot and its simple name. Thus, in this example, com.jdojo.intro.Welcome is the fully qualified name of the class.
The next question that might arise in your mind is “What is the fully qualified name of a class that does not have a
package declaration?” The answer is simple. In such a case, the simple name and the fully qualified name of the class
are the same. If you remove the package declaration from the source code, Welcome will be both names for your class.
You might notice that the name of the class ( Welcome ) and the name of the file you saved the source code in
( Welcome.java ) match (excluding the file extension .java ). This did not happen by chance. I did select the file name
Welcome.java on purpose. Is it required to name the source code file the same as the name of the class? Yes, but not
always. This raises another question. What should be the name of the source code file if you declare two classes in the
same source code? Suppose you declare two classes, Welcome and Bye , in your source code. What name should you
give to the source file: Welcome.java , Bye.java , or something else? In some cases, Java forces you to keep the name
of a class and the file name the same. In your example, you do not need to stick to any source code file-naming rules.
It is correct even if you name your source file as Bye.java or Test.java . However, let's continue with this example,
assuming that the source code has been saved in the file Welcome.java .
A fully qualified name.
 
Search WWH ::




Custom Search