Java/J2EE and SOA (348 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (16 Blogs)
  • Comprehensive Java Course (4 Blogs)
  • Java/J2EE and SOA (345 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

What is the basic Structure of a Java Program?

Last updated on Sep 27,2023 49K Views

7 / 11 Blog from Introduction to Java

Java programming language is platform-independent and a secure programming language. With various applications, 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 blog:

Documentation Section

structure of a program - structure of a java program - edureka

It is used to improve the readability of the program. It consists of comments in Java which 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

A provision in Java allows you to declare your classes in a collection called package. There can be only one package statement in a Java program, which must 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 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 inheritance 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 can also be used outside of this class.

  • 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. You can pass the input parameter if you run the Java code through a console. 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 found this article on “Structure Of A Java Program” relevant, check out Edureka’s Java Certification Training, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. 

We are here to help you with every step on your journey. We have compiled the list of Top Java Interview Questions and developed a curriculum designed for students and professionals who want to be Java developers. The course is designed to give you a head start into Java programming and train you for core and advanced Java concepts and various Java frameworks like Hibernate & Spring.

If you come across any questions, feel free to ask all your questions in the comments section of “Structure of a Java Program” and our team will be glad to answer.

Upcoming Batches For Java Certification Training Course
Course NameDateDetails
Java Certification Training Course

Class Starts on 27th April,2024

27th April

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

What is the basic Structure of a Java Program?

edureka.co