What is the Basic Structure of a Java Program?

Bhavitha Thippanna
Edureka
Published in
6 min readSep 12, 2019

Java programming language is platform-independent and a secure programming language. With a wide variety of applications, the Java programming language has been in demand for the last two decades. The out-of-the-box features help java stand apart. In this article, we will understand the structure of a java program in detail. Following are the topics discussed in this article:

  • Documentation Section
  • Package Statement
  • Import Statement
  • Interface Section
  • Class Definition
  • Main Method Class

Documentation Section

It is used to improve the readability of the program. It consists of comments in Java that include basic information such as the method’s usage or functionality to make it easier for the programmer to understand it while reviewing or debugging the code. A Java comment is not necessarily limited to a confined space, it can appear anywhere in the code.

The compiler ignores these comments during the time of execution and is solely meant for improving the readability of the Java program.

There are three types of comments that Java supports

  • Single line Comment
  • Multi-line Comment
  • Documentation Comment

Let’s take a look at an example to understand how we can use the above-mentioned comments in a Java program.

// a single line comment is declared like this
/* a multi-line comment is declared like this
and can have multiple lines as a comment */
/** a documentation comment starts with a delimiter and ends with */

Package Statement

There is a provision in Java that allows you to declare your classes in a collection called package. There can be only one package statement in a Java program and it has to be at the beginning of the code before any class or interface declaration. This statement is optional, for example, take a look at the statement below.

package student;

This statement declares that all the classes and interfaces defined in this source file are a part of the student package. And only one package can be declared in the source file.

Import Statement

Many predefined classes are stored in packages in Java, an import statement is used to refer to the classes stored in other packages. An import statement is always written after the package statement but it has to be before any class declaration.

We can import a specific class or classes in an import statement. Take a look at the example to understand how to import statement works in Java.

import java.util.Date; //imports the date class
import java.applet.*; //imports all the classes from the java applet package

Interface Section

This section is used to specify an interface in Java. It is an optional section which is mainly used to implement multiple inheritances in Java. An interface is a lot similar to a class in Java but it contains only constants and method declarations.

An interface cannot be instantiated but it can be implemented by classes or extended by other interfaces.

interface stack{
void push(int item);
void pop();
}

Class Definition

A Java program may contain several class definitions, classes are an essential part of any Java program. It defines the information about the user-defined classes in a program.

A class is a collection of variables and methods that operate on the fields. Every program in Java will have at least one class with the main method.

Main Method Class

The main method is from where the execution actually starts and follows the order specified for the following statements. Let’s take a look at a sample program to understand how it is structured.

public class Example{
//main method declaration
public static void main(String[] args){
System.out.println("hello world");
}
}

Let’s analyze the above program line by line to understand how it works.

public class Example

This creates a class called Example. You should make sure that the class name starts with a capital letter, and the public word means it is accessible from any other classes.

Comments

To improve the readability, we can use comments to define a specific note or functionality of methods, etc for the programmer.

Braces

The curly brackets are used to group all the commands together. To make sure that the commands belong to a class or a method.

public static void main

  • When the main method is declared public, it means that it can be used outside of this class as well.
  • The word static means that we want to access a method without making its objects. As we call the main method without creating any objects.
  • The word void indicates that it does not return any value. The main is declared as void because it does not return any value.
  • Main is the method, which is an essential part of any Java program.

String[] args

It is an array where each element is a string, which is named as args. If you run the Java code through a console, you can pass the input parameter. The main() takes it as an input.

System.out.println();

The statement is used to print the output on the screen where the system is a predefined class, out is an object of the PrintWriter class. The method println prints the text on the screen with a new line. All Java statements end with a semicolon.

This brings us to the end of this article where we have learned about the structure of a Java program. I hope you are clear with all that has been shared with you in this tutorial. If you wish to check out more articles on the market’s most trending technologies like Artificial Intelligence, DevOps, Ethical Hacking, then you can refer to Edureka’s official site.

Do look out for other articles in this series that will explain the various other aspects of Java.

1. Object Oriented Programming

2. Inheritance in Java

3. Polymorphism in Java

4. Abstraction in Java

5. Java String

6. Java Array

7. Java Collections

8. Java Threads

9. Introduction to Java Servlets

10. Servlet and JSP Tutorial

11. Exception Handling in Java

12. Advanced Java Tutorial

13. Java Interview Questions

14. Java Programs

15. Kotlin vs Java

16. Dependency Injection Using Spring Boot

17. Comparable in Java

18. Top 10 Java frameworks

19. Java Reflection API

20. Top 30 Patterns in Java

21. Core Java Cheat Sheet

22. Socket Programming In Java

23. Java OOP Cheat Sheet

24. Annotations in Java

25. Library Management System Project in Java

26. Trees in Java

27. Machine Learning in Java

28. Top Data Structures & Algorithms in Java

29. Java Developer Skills

30. Top 55 Servlet Interview Questions

31. Top Java Projects

32. Java Strings Cheat Sheet

33. Nested Class in Java

34. Java Collections Interview Questions and Answers

35. How to Handle Deadlock in Java?

36. Top 50 Java Collections Interview Questions You Need to Know

37. What is the concept of String Pool in Java?

38. What is the difference between C, C++, and Java?

39. Palindrome in Java- How to check a number or string?

40. Top MVC Interview Questions and Answers You Need to Know

41. Top 10 Applications of Java Programming Language

42. Deadlock in Java

43. Square and Square Root in Java

44. Typecasting in Java

45. Operators in Java and its Types

46. Destructor in Java

47. Binary Search in Java

48. MVC Architecture in Java

49. Hibernate Interview Questions And Answers

Originally published at https://www.edureka.co on September 12, 2019.

--

--