Java Programming

Structure of Java Program

Java language is pretty much different in comparison to C & C++ in terms of its programming structure. In this post, we will refer through the programming structure used in Java programming with proper examples.

A Java program may contain 7 blocks according to the requirement. These blocks are as follows

  • Documentation Section
  • Package Statement
  • Import Statements
  • Import Statements
  • Interface Statements
  • Class Definitions
  • Main Method Class

Structure of Java Program

Structure of Java programming

Documentation Section

Documentation section in Java programming is an optional but suggested block among 7 blocks. This section is used to add details related to program i.e., what is the content of the program, who is the author of the program, the algorithm used in the program, etc. It is as similar to the comments in the programs. Also, the comments used in the declaration section is known as documentation comments.

Package Statements

This block of Java programming is used to add the package in Java. A package is a collection of predefined classes, interface, etc which we use in our program to make it more better.

The package statement simply declares with the keyword “package”.

For example – package student;

This package student may be a class or enumeration or any other predefined element which we may use in our program by just declaring its name.

Import statement

Import statements are pretty much similar in comparison to C & C++ header files. Suppose a package student may contain various classes such as marks, branch, etc and we want to import the class “marks” in our program. Then this can be done with the help of the import statement.

The import statement can be done using the keyword “import”.

For example – import student.marks

Here marks is a class of package student.

Interface Statement

An interface is pretty much similar to the classes but comes with little bit modifications. An interface is the combination of data member ( i.e., data type ) and member functions ( i.e., functions ).

Interface statement is an optional statement used in Java programming. We may use predefined Interface statement using the keyword “interface”.

For Example:

interface student
{
  Variable declaration;
  Method declaration;
}

Also read,

Class Definitions

A class is a user-defined collection of data member ( i.e., data type ) and member function ( i.e., functions ). We may create as many class as we want in Java. This is an optional block in Java programming.

A class must have to declare before main method class. It can be declared using the keyword “class”.

For Example-

class employee
{
  Data members;
  Member Functions;
}

Main Method Class

The implementation of actual programming starts from here. It is pretty similar to C & C++ main() function. This class is the main class of the program. Main method class is the essential block among all the blocks. This block contains the group of statements or codes which we actually use to build our program.

Main method class must follow certain statement to use in the program i.e., public static void main(String args[ ])

Example-

public static void main(String args[])
{

  Code Line 1;
  Code Line 2;
   ......
   ......
   ......
  Code Line n;

}

 


A simple Java program to demonstrate the structure of Java program

We are taking a simple example to print a message using a Java statement.

class test
{

   publie static void main(String args[])
   {
 
    System.out.println("Hello Java");

   }

}

Class Declaration

Take a look at the first line of our code i.e., class test. Here the class can be declared using the keyword class and the test is identifier i.e., we have given the name of our class test.

In Java, we have to place our actual main method inside a class. So we must need to create a class before the declaration of main method.

Main Method Declaration

Main method declaration is pretty much similar to C & C++. It contains the actual code of the program. Consider the line public static void main(String args[ ])

This block contains 3 keywords i.e., public, static and void

  • Public: Here the public is the access specifier which allows using the data members and the member function of the program outside the class. You may refer difference between access specifier and access specifier.
  • Static: Static is a reserved keyword which we must need to declare with the main() function to tell that we may use methods either the object of class exits or not.
  • Void: void is a type modifier which tells that the main() function does not return any value.
  • System.out.println: This statement is simply used to display the output. In our above example, we used system.out.println to display the message “Hello Java”.

ALSO READ

Leave a Reply