Dissecting a Java Program

Dissecting a Java program

Do you know why do you have public or static or void or main() in a Java program? Can we use something else like private in place of public? In this post, we are dissecting a java program to understand these keywords? Every component of a java program (except your application code) serves a purpose and your program won’t run without it.

Our Java code

Lets take a simple java program to take this post dissecting a java program ahead. Our java program calculates simple Interest by taking user inputs.

package Javatutorials;

import java.util.Scanner;

public class calculateSI {

public static void main(String[] args) {

//Get user inputs

Scanner in = new Scanner(System.in);

System.out.println(“Enter the principal amount:”);
int p = in.nextInt();

System.out.println(“Enter the rate of interest:”);
int r = in.nextInt();

System.out.println(“Enter the duration in years:”);
int t = in.nextInt();

////Calculate Simple Interest

int si = p * r * t / 100;

////Print Simple Interest

System.out.println(“The simple interest is : ” + si);

in.close();

}

}

We are going to dissect the first few lines of the code, which is common across all the java programs. The remaining section of the code is specific to your application logic and that will change based on your business need. The focus of this post is to dissect the common part of Java code, which is:

package Javatutorials;

import java.util.Scanner;

public class calculateSI {

public static void main(String[] args) {

We will start by looking at the last one first.

What is main()?

The function main() is known as an entry point or gate to your program. Java programs are executed by Java virtual machine (JVM). JVM looks for the main() function to begin the execution of your program.

Dissecting Java program - entry

In the absence of main() function, your program will not execute. Can you choose to have a different entry function to your program? No, to keep it standardized, main() is the only entry point. However, JavaFX applications are an exception.

I tried to run a java program by renaming main() to main2(), I got the following error (I have removed the second part of the message for simplicity sake):

Error: Main method not found in class Javatutorials.calculateSI, please define the main method as:
public static void main(String[] args)

What is void?

Void is the keyword used before main(). Any keyword used immediately before a function indicates the return value. Return values indicate “what to expect in return”, if we run this function. Any valid datatype like integer, String, Float etc could be a return type.

What if we have no return value from a function or it does not need to return any value? In that case, we are going to use void. It indicates that don’t expect anything in return.

Why main() is using a return type as void? There is a perfect sense for it. JVM is calling this function. Once the program execution ends, JVM can’t do anything with its return value as that’s not useful for JVM. The program itself has ended.

As far as, knowing the status of the running program is concerned, JVM has other mechanisms to do so.

What is static?

The next in line is static. In Java, a function/method belongs to a class. So, a function of a class will only be available after it is created (or instantiated). In case of main(), your class is not yet instantiated. Look at the first two lines of code:

public class calculateSI {

public static void main(String[] args) {

In the above code, main() function is immediately after the public class calculateSI declaration. The only way out is to declare it as static. Static functions are available to be used even before the class is instantiated.

Read also: What does static keyword do in a class?

What is public?

The leftmost keyword is public. This means that this function is available to every calling program or any external program can call it. This one is simple. JVM is an external program and unless you declare the main function as public, JVM can’t access it.

public is known as access specifier in Java. Other access specifiers are protected and private.

What is String[] args?

The right most part of main() function is its argument. These values can be used to call main() function. It means that the calling program can pass on these values to the main() function. main() function can use these values to complete its work.

String[] args are input values which can be passed to main() function. But how? These values can be passed on as command line arguments.

java calculateSI one two

This is how command line arguments can be used. As String[] args is an array, we can get the values of command line arguments as args[0] and args[1].

Remaining parts of the program

Other parts of every java program are as shown below:

package Javatutorials;

import java.util.Scanner;

public class calculateSI {

Package statement defines your own packages. Packages are nothing but collection of programs logically grouped together.

Import statement refers to java libraries. Java provides hundred of libraries with pre-built functions. You don’t need to include all of them in every program. Instead, you can only import the required ones.

Like in our example,  we have used java.util.Scanner library. This library provides functions for accepting the user input.

Finally, public class calculateSI is the declaration for your class with access specifier as public. This class has to be public because it is be accessed by JVM.

Hope this article has helped you in understanding the java program anatomy.

About Techcanvass

Techcanvass is an IT training academy for professionals offering courses in Business Analysis, automation testing and analytics. Techcanvass is an IIBA authorized training centre and offers business analysis certification courses along with selenium certification courses. Being an authorized Endorsed education provider, we offer IIBA certification preparation courses.

Selenium Training Course
Selenium Training Course
Selenium Certification Course
Selenium Certification Course

Leave a Reply

Your email address will not be published. Required fields are marked *