- Basic Structure of Java Program
- Class demonstrating basic structure of java program.
- Package Statement in Java
- Import Statements in Java
- Documentation or comment in Java
- Does these documents or comments executed while compiling or executing the program ?
- Class Definition in Java
- Declare static and non static variables
- Declare constructors
- Declare main method or other methods
- What if I need to declare an interface in my program ?
- Java Program Structure
- Basic Structure of Java Programs
- A Simple Java Program to Print «Hello Java»
- Let’s Look into Various Parts of the Above Java Program
- Explain the basic structure of a program in Java?
- Import statements
- Comments
- Class Definition
- Variables
- Main Method
- Methods/Behaviors
- Example
- Output
Basic Structure of Java Program
This tutorial explains the basic structure of java program. It covers a brief detail of common components of a java program and the order in which they should be declare/defined inside a java program. Writing the program in a structured way helps you and your team mate to understand the program easily.
Some of the orders of components are mandatory to follow while others are not, but it’s a good practice to follow the orders given in this tutorial as it makes your program more readable. The basic components of a java program and their order of declaration is :
- Package Statement
- Import Statement
- Documentation or comment
- Class Definition
- Declare instance and static variables
- Declare constructors(if any)
- Main method or other methods
Before we start to understand these components, let’s see a program demonstrating above structure first, then we will see each component one by one.
Class demonstrating basic structure of java program.
package
mypack;import
java.util.Date;/** * The ProgramStructure class used to demonstrate * the structure of java program. * * @author refreshjava */
class
ProgramStructure </* The variable date is used to store the current date. */
public static
Date date = new Date();// This variable stores website name.
private
String siteName ="refreshjava.com"
;/** * The no argument constructor. */
public void
ProgramStructure() < System.out.println("No argument constructor"
); >/** * This method prints the string passed to it in console. * * @param str The string to be printed */
public void
printMessage(String str) < System.out.println(str); >/** * Entry point, it's the first method to be called * while running this class. */
public static void
main(String args []) < ProgramStructure obj =new
ProgramStructure(); obj.printMessage("Java program of code structure"
); System.out.println("Program was written on string">"Program author : "
+obj.siteName); > >Java program of code structure
Program was written on = Tue Apr 28 09:04:36 IST 2020
Program author : refreshjava.comTo run this program you must have knowledge about packages in java, if not refer packages in java tutorial first.
Package Statement in Java
A package is a group of similar types of classes and interfaces. A package name is basically a directory in your computer where the given class/program is saved. It’s an optional statement, if you don’t declare any package, your program will be saved inside a default package(no name package). Package declaration in above program is :
There can be only one package statement inside a program and it must be the first line of code in your program. The program must be saved inside that package(directory), for example the above program must be saved inside mypack directory. To get more detail about packages refer packages in java tutorial.
Import Statements in Java
If your program wants to access classes of any other packages, then those classes or packages must be imported in your program first. Import statements basically imports/includes the classes of a packages in your program, once imported you can use the classes of that packages in your program.
All the Import statements must be written after package name declaration and before class name declaration. We can import a single class or all classes of a package like below :
import
java.util.Date;//imports only Date class
import
java.util.*;//imports all the classes of java.util package
To get more detail about import statements, refer package import in java tutorial.
Documentation or comment in Java
Documentation or comment is basically a little detail about your components(class, method, variable etc) like what it does or what it is used for etc. Documentation makes it easy for you and your team mates to understand your program easily. Documentation or comment about a component should be written just before it’s declaration.
Though it’s optional to write the documentation or comment but it’s a good practice to write them at the time of designing the component. It’s not necessary to write the documentation for each and every element of a program. Java provides the following approaches to write documentation or comments in your programs.
- Single line comment : It starts with two forward slashes // and followed by your comment. Any text after // and till the end of line is considered as comment. You can use single line comments anywhere and up to any number of times in your program.
// This is single line comment
/* This is multi line comment This is multi line comment */
/** * This is documentation, used to give * the little detail of component. */
Does these documents or comments executed while compiling or executing the program ?
No, Java completely ignores them, which means they are not executed while compiling or executing the program.
Note : While learning java, you can avoid to use comments as it takes time but if you are working on any project or application then you should follow to write the documents or comments about different components of your program as it will make your program more readable.
Class Definition in Java
In java, a program is basically a group of one or more classes. We define a class using class keyword, followed by class name. As a good practice programmer should write a meaningful name to their classes. Everything given inside < >after class name is the part of that class.
As a good practice you should write little detail about your class before it’s declaration, as given in above program. To get more detail about classes refer classes in java tutorial.
Declare static and non static variables
Inside the class, first you should declare all your static and non static variables of that class. Though it’s not mandate, but it’s a good practice to define the variables before defining methods or constructors of a class. It makes easy to understand your program. To get details about variables refer variables in java tutorial.
Declare constructors
After variables you should define the constructors(if any) of that class, again it’s not mandatory, it’s just a good practice to follow. You should define all your constructors before defining the methods. Also write little detail about the constructors as given in above example to make it easy to understand that constructor. To get detail about constructors refer constructors in java tutorial.
Declare main method or other methods
After defining constructors, you should define the main method and other methods of that class. You should also write little detail about the method before it’s declaration to make it easy to understand. In java, to run a program independently it must have a main method like below :
public static void
main(String args []) <// method body
>The main method is the entry point of a program which means it’s the fist method which will be called while running your program. To get detail about methods refer methods in java tutorial.
What if I need to declare an interface in my program ?
Generally interfaces are declared in a separate file, but if needed you can declare it before class declaration as interfaces are designed to be implemented by classes. Though it’s not mandatory to declare before class but doing so makes your program more readable.
- Keep in mind that java keywords and code is case sensitive.
- Generally You need to define a class to write a program in java.
- You must save your program with same name as class name having main method.
- A program may have only variables, methods or constructors.
- Generally each class in java is defined in separate file but we can define multiple classes in same file.
Java Program Structure
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
- Documentation Section
- Package Statement
- Import Statements
- Interface Statement
- Class Definition
- Main Method Class
- Main Method Definition
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»
//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"); >>
- 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
- 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.
- 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.
Explain the basic structure of a program in Java?
A class in Java can be placed in different directories/packages based on the module they are used. For all the classes that belong to a single parent source directory, a path from source directory is considered as package declaration.
Import statements
There can be classes written in other folders/packages of our working java project and also there are many classes written by individuals, companies, etc which can be useful in our program. To use them in a class, we need to import the class that we intend to use. Many classes can be imported in a single program and hence multiple import statements can be written.
Comments
The comments in Java can be used to provide information about the variable, method, class or any other statement. It can also be used to hide the program code for a specific time.
Class Definition
A name should be given to a class in a java file. This name is used while creating an object of a class, in other classes/programs.
Variables
The Variables are storing the values of parameters that are required during the execution of the program. Variables declared with modifiers have different scopes, which define the life of a variable.
Main Method
Execution of a Java application starts from the main method. In other words, its an entry point for the class or program that starts in Java Run-time.
Methods/Behaviors
A set of instructions which form a purposeful functionality that can be required to run multiple times during the execution of a program. To not repeat the same set of instructions when the same functionality is required, the instructions are enclosed in a method. A method’s behavior can be exploited by passing variable values to a method.
Example
package abc; // A package declaration import java.util.*; // declaration of an import statement // This is a sample program to understnd basic structure of Java (Comment Section) public class JavaProgramStructureTest < // class name int repeat = 4; // global variable public static void main(String args[]) < // main method JavaProgramStructureTest test = new JavaProgramStructureTest(); test.printMessage("Welcome to Tutorials Point"); > public void printMessage(String msg) < // method Date date = new Date(); // variable local to method for(int index = 0; index < repeat; index++) < // Here index - variable local to for loop System.out.println(msg + "From" + date.toGMTString()); > > >
Output
Welcome to Tutorials Point from 2 Jul 2019 08:35:15 GMT Welcome to Tutorials Point from 2 Jul 2019 08:35:15 GMT Welcome to Tutorials Point from 2 Jul 2019 08:35:15 GMT Welcome to Tutorials Point from 2 Jul 2019 08:35:15 GMT