Java Programming Tutorial Index

Java Overview

It is necessary to know the exact structure of the Java program, and this lesson contains a detailed description of it. This lesson is essential for you before proceeding to learn more advanced lessons of Java programming. Here, in this chapter, you will study the structure of the Java program. Such as how to create a simple Java program and what its different sections mean.



Java program structure means - the way to write a java program or general format.

Basic Structure of Java Programs

A Java program involves the following sections:
  • Documentation Section
  • Package Statement
  • Import Statements
  • Interface Statement
  • Class Definition
  • Main Method Class
    • Main Method Definition
Section Description
Documentation Section You can write a comment in this section. Comments are beneficial for the programmer because they help them understand the code. These are optional, but we suggest you use them because they are useful to understand the operation of the program, so you must write comments within the program.
Package statement You can create a package with any name. A package is a group of classes that are defined by a name. That is, if you want to declare many classes within one element, then you can declare it within a package. It is an optional part of the program, i.e., if you do not want to declare any package, then there will be no problem with it, and you will not get any errors. Here, the package is a keyword that tells the compiler that package has been created.

It is declared as:

package package_name;
Import statements This line indicates that if you want to use a class of another package, then you can do this by importing it directly into your program.

Example:

import calc.add;
Interface statement Interfaces are like a class that includes a group of method declarations. It's an optional section and can be used when programmers want to implement multiple inheritances within a program.
Class Definition A Java program may contain several class definitions. Classes are the main and essential elements of any Java program.
Main Method Class Every Java stand-alone program requires the main method as the starting point of the program. This is an essential part of a Java program. There may be many classes in a Java program, and only one class defines the main method. Methods contain data type declaration and executable statements.

Here is an example of the Hello Java program to understand the class structure and features. There are a few lines in the program, and the primary task of the program is to print Hello Java text on the screen.

A Simple Java Program to Print "Hello Java"

Example:

//Name of this file will be "Hello.java"

public class Hello
{   
    /* Author: www.w3schools.in
    Date: 2018-04-28
    Description:
    Writes the words "Hello Java" on the screen */

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

Program Output:

Hello Java

Here are the most important points to note about the Java programs:

  • You have to keep in mind that, Java code is case sensitive.
  • To write a Java program, you must have to define class first.
  • The name of the class in Java (which holds the main method) is the name of the Java program, and the same name will be given in the filename. As mentioned above in the sample program; The name of the class is "Hello" in which the main method is, then this file will be named "Hello.Java".

Let's Look into Various Parts of the Above Java Program

public class Hello
  • This creates a class called Hello.
  • All class names must start with a capital letter.
  • The public word means that it is accessible from any other classes.
/* Comments */ The compiler ignores comment block. Comment can be used anywhere in the program to add info about the program or code block, which will be helpful for developers to understand the existing code in the future easily.
Braces Two curly brackets {...} are used to group all the commands, so it is known that the commands belong to that class or method.
public static void main
  • When the main method is declared public, it means that it can also be used by code outside of its class, due to which the main method is declared public.
  • The word static used when we want to access a method without creating its object,  as we call the main method, before creating any class objects.
  • The word void indicates that a method does not return a value. main() is declared as void because it does not return a value.
  • main is a method; this is a starting point of a Java program.

You will notice that the main method code has been moved to some spaces left. It is called indentation which used to make a program easier to read and understand.

String[] args It is an array where each element of it is a string, which has been named as "args". If your Java program is run through the console, you can pass the input parameter, and main() method takes it as input.
System.out.println(); This statement is used to print text on the screen as output, where the system is a predefined class, and out is an object of the PrintWriter class defined in the system. The method println prints the text on the screen with a new line. You can also use print() method instead of println() method. All Java statement ends with a semicolon.


Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram