All java source files must end with the extension ".java". A source file may contain at the most one top level public class. If a public class is present, the un-extended file name should be same as the class name i.e. if the class name is Hello then the source file name should be Hello.java. If a source file does not contain any public class then name of the source file can be anything.
A source file may contain an unlimited number of non-public class definitions.
There are four top level elements that may appear in a file. None of these elements is must. If they are present, then they must appear in the following order:
A source file may have zero or more import statements. As of now you can think of import statements to be like #include statements in the Java Program. If present, all the import statements must come after the package statement and before the class/interface definitions.
A source file may have any number of class and/or interface definitions but there can be at the most one public class or interface.
main() method
A source file may or may not contain a class having main() method but a standalone java program always starts its execution from main() just like a C/C++ program. So the class from which we want to start the execution must have the main() defined in it.
The main() method must always have the following signature:
public static void main(String args[])
Note: Method prototype is referred to as signature in Java.
The void keyword preceding main() method indicates that it does not return any value. The keyword public indicates that the main method can be accessed from anywhere. The main() method must be declared public as it is called by the code, which is part of the JVM and is outside the class containing the main() method.
The main() method takes one argument, which is the array of strings. Each element of the array represents one command line argument.
The main() method can also be invoked like any other static method form anywhere in the java program. It can also be overloaded like any other Java method.
It is very common to get an exception indicating that the main() method is not present while running a java program event when the main() method is present. This happens when the signature of the main() method differs from the above mentioned signature.
Comments
A program can be documented by inserting comments wherever required. The comments are simply ignored by the compiler.
Java provides three types of comments for documentation:
Single line CommentA source file may contain an unlimited number of non-public class definitions.
There are four top level elements that may appear in a file. None of these elements is must. If they are present, then they must appear in the following order:
- Package definitions
- import statements
- class and/or interface definitions
A source file may have zero or more import statements. As of now you can think of import statements to be like #include statements in the Java Program. If present, all the import statements must come after the package statement and before the class/interface definitions.
A source file may have any number of class and/or interface definitions but there can be at the most one public class or interface.
main() method
A source file may or may not contain a class having main() method but a standalone java program always starts its execution from main() just like a C/C++ program. So the class from which we want to start the execution must have the main() defined in it.
The main() method must always have the following signature:
public static void main(String args[])
Note: Method prototype is referred to as signature in Java.
The void keyword preceding main() method indicates that it does not return any value. The keyword public indicates that the main method can be accessed from anywhere. The main() method must be declared public as it is called by the code, which is part of the JVM and is outside the class containing the main() method.
The main() method takes one argument, which is the array of strings. Each element of the array represents one command line argument.
The main() method can also be invoked like any other static method form anywhere in the java program. It can also be overloaded like any other Java method.
It is very common to get an exception indicating that the main() method is not present while running a java program event when the main() method is present. This happens when the signature of the main() method differs from the above mentioned signature.
Comments
A program can be documented by inserting comments wherever required. The comments are simply ignored by the compiler.
Java provides three types of comments for documentation:
- Single line comment
- Multi line comment
- Documentation comment
A single line comment spans only one line. It can be given on any line followed by the character "//". The syntax is similar to C++.
Example
// This line is a comment and will be ignored by the compiler
x = 4*y // This is also a comment but appears after assignment statement
Multi line Comment
The syntax of multi line comment is same as C/C++. A multi line comment is enclosed between "/*" and "*/".