The Best Technical Blog Ever

twitterrss

Structure of Java Program

This basic structure tells you how to write java program. A java program structure consists of six different parts. Some of them are optional sections. It purely depends on user and the requirements of program. If not required, then the optional sections can be excluded from the program.


java structure

Documentation section:

It is a set of comment lines explaining the program's purpose and improves the readability of the program. It is a non-executable statement that helps to understand a program especially when your programs get more complex. It exists only for the programmer and is ignored by the compiler. A good program should contain comments that describe the information which the programmer would like to refer e.g. purpose of the program, author name, date and time of program creation. Such comments begin with delimiter /** and end with */. The compiler also ignores these comments. The main purpose is to automatically generate program documentation. This section is optional.


/**The text enclosed here will be part of program documentation */

Package declaration:

Java allows you to group classes in a collection known as package. This statement declares a package name and informs the compiler that the classes defined here belong to this package. The package declaration consists of the keyword package, a space, and the name of the package . It must appear as the first statement in the source code before any class or interface declaration. Only one package declaration can appear in the source file. Package declaration is optional. So as per the applications need, we can simply omit this statement.


Example:
package staff;

This statement declares that all classes and interfaces defined in this source file are part of the staff package.


Import statements:

It is Similar to #include statement in C. In java, we have lot of predefined classes that are stored into packages. To refer these predefined classes, you have to use fully quallified name like packagename.classname. Import statements are used to access the classes and interfaces that are part of other packages. An import statement tells the Java compiler, this Java file is using which other Java files contained in that package. It needs to retype the package path name along with the classname. We can omit this by using import keyword. It is just a simple way of cutting down on the amount of typing, programmers have to do in order to use other classes. Here, a java file only needs to import Java files which are not located in the same Java package. An import statement is used for referring classes that are declared in other packages. The import statement is written after a package statement but before any class definition. If we want to use classes of multiple packages in that case we can use multiple import statement., each on its own line.


Example
import java.util.Scanner; //This statement imports only the Scanner class from java.util package.
import java.util.*; //This statement imports all the classes from java.util package.

This statement declares that all classes and interfaces defined in this source file are part of the staff package.


Interface statement:

It is similar to a class but only includes constants and group of method declaration. It is used in java, when we want to implement multiple inheritance feature. Interfaces cannot be instantiated. They can only be implemented by classes or extended by other interfaces. It is an optional section.


Example:
interface ExampleInterface
{
   public void method1();  // All the methods are public abstract by default
   public void method2();  //Note down that these methods are not having body
}

Class definition:

It describes information about user defined classes of the program. Classes are primary feature of Java program. Every Java program consists of at least one class definition with main method which is starting point of program execution and follows the statements in the order specified. A class is a collection of data members(variables) and methods that operate on the fields. Java program may contain multiple class definition. They are used to map real world problems. Java program may contain multiple class definition. Class is the compulsory section.



Main Method Class:

It is the starting point of the program. The main method can have the ability to create objects, evaluate expressions, and invoke other methods. On reaching the end of main, the program terminates. and the control passes back to the operating system. Java Compiler will compile class that do not contain main() method. If main() is not declared, it reports runtime error, because at compile time Compiler is not using main() but interpreter use it at runtime.


Example for Java Structure:
structure

Basic Syntax:

  • Case Sensitivity : Java is case sensitive. For example, the command RUN and run would have different meaning in Java. A case-sensitive program that it expects you to enter all commands in uppercase will not respond correctly if you enter one or more characters in lowercase.

  • Class Names : For all class names the first letter should be in Upper Case. If several words are used to form a name of the class, each inner word's first letter should be in Upper Case.
    Example:
    class SimpleJavaProgram

  • Method Names :All method names should start with a Lower Case letter. If several words are used to form the name of the method, then each inner word's first letter should be in Upper Case.
    Example:
    public String myMethodName()

  • Program File Name :Name of the program file should exactly match the class name. and saved with '.java' extension to the end of the classname (if the file name and the class name do not match, then the program will not compile). If the program consist of multiple classes then the class containing main method should be used as a file name.

    public static void main(String args[]){} :-

    Class must contain at least one main method. Java program processing starts from the main() method.

Keypoints to remember in java

1. Java is a high level, robust, secured , platform independent and object oriented programming.... more info

Back to Basics:- Java Syntax

In this session, you are going to refresh some basic set of rules(java syntax), defining how a ... more info

Total Visits
Traffic feed status