|
Java source file
§ Java source file can have multiple top-level classes.
§ Only "one" public top-level class is allowed per java source file.
§ The name of the java source file and the name of the top-level public class must be same.
§ If there is no (not a single one) top-level public class present in a java source file, then the java source file's name can be anything.
§ After compilation of such file, class files are generated for all classes with their respective names.
Package, import statements and Class declaration
§ Package statement, import statements and class declarations are optional in java source file.
§ If present, they must appear in the order given.
1. Package declaration
2. Import statements
3. Class declarations
§ There can be comments or white space in between these declarations.
The main() method
§ A class with a main method can be invoked from command line as a java application.
§ The signature of main method is public static void main(String[] args)
§ The main() method takes String array as argument. You can pass command line arguments to the application through this array.
Initialization of variables
§ Java variables are of two broad types: member variables and local variables.
§ Only member variables are initialized automatically. The local variables including the method variables need explicit initialization.
§ Member variables are instance variables and static variables. The instance variables are initialized automatically when an instance is created and static variables are automatically initialized when the class is loaded.
§ The default values assigned during the automatic initialization depend on the variable type. The object reference variables are initialized to a null value.
§ Local variables are declared either in a method body or in a code block. They are not initialized automatically. A java programmer must explicitly initialize them before their first use.
Interface Implementation
§ A class can implement multiple interfaces. In other words, you can specify multiple interfaces in the implements clause of class declaration.
§ A class cannot extend from an interface. In other words, you cannot specify interface name in the extends clause of class declaration.
§ An interface can only extend from another interface. This means that you cannot specify class name in the extends clause of the interface declaration.
§ A class can be extended from only one class but an interface can be extended from multiple interfaces.
§ When a java class says that it implements particular interface, it must implement all the methods in that interface or should declare it self as abstract.