Information in a java program is organized in a specific way. The smallest and simplest java program can only execute if it is coded as a .java file. When a .java file is created, it must contain at least one class. A java class contains specific information such as variables and methods. A single java file is generally structured similarly:
public class Greeting
{
int someVar = 0;
public static void main(String[] args) {
System.out.println(“Hello, world!”);
}
}
In this classic “Hello, world!” output, the class Greeting is defined. A .java file must contain and define at least one class. A class can be controlled by methods defined within it. In this example, a special method main will try to show the output “Hello, world!” when the class is loaded. A class may contain fields such as the integer type of variable declared as someVar, with the value of zero. As each .java file should have one class, it should also have one class that has the main method. Greeting, someVar, and main are called identifiers. An identifier labels entities in a java program such as a class, a variable, or a method. The class Greeting is a public class. Each java file can have many classes but only one can be declared public. Note also that the method main is public, and static.
Many .java files may be actually needed to run a more complex program. Similarly, some methods and classes are meant to be reused in some other way for various programming applications. In this case, it is best to organize .java files according to their use, and group programs that generally work for the same kind of output. If a greetprogram.java file contains:
package sayinghello;
public class Greeting
{
public static void main(String[] args) {
System.out.println(“Hello, world!”);
}
}
Here, the Greeting class is bundled to a package called sayinghello. The keyword package must be at the first line of a .java file. Classes from the sayinghello package can be used by classes from another program by using import:
//this is a file saved as mymeeting.java
import sayinghello;
public class Meeting
{
public static void main(String[] args) {
sayinghello.Greeting();
System.out.println(“Nice to meet you, goodbye!”);
}
}
By importing the saying hello package, this simplifies the need to re-code the entire “hello world” program. The commonly used programs are already packaged for easy access for programmers, they usually come bundled with software development kit (SDK). The open-source community thrives in collaboration and sharing, packages help keep even large and complex programs from chaos. In even more complex programs, packages may need to interact with other packages. Packages can also contain packages within them, which are called subpackages.
Lastly, packages and subpackages are stored in the file system within folders and sub-folders respectively. An entire collection of packages and subpackages can be archived into a single file called a java archive. The file extension .jar is associated with a java archive.
Some mobile devices such as Cellular phones and PDAs that use the Java 2 Platform, Micro Edition (J2ME), require a pair of files to run a java program. Aside from the collected .jar file, a second file called a Java application descriptor (.jad). The .jar and .jad, together encompasses a MIDlet suite. A .jad file is not part of the program itself, but it supplies additional information or metadata to describe the actual program contain in the .jar file.
Nice site – Wishing you a very happy and prosperous new year !