Skip to content
Code To Live By » Java Tutorial » Basic Program Structure / Anatomy of a Java file

Basic Program Structure / Anatomy of a Java file

  • by

So now that we’ve made our first Java program let’s talk a little about how to structure our program, as well as the basic building blocks of the Java programming language.

As a refresher, our Hello World program looked something like this:

package code.to.live.by;

public class HelloWorld {

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

}

Packages and Imports

Starting with line 1, we have placed our Hello World program into the “package” of code.to.live.by

Packages provide us with a way to organize our code, we can group related classes together and avoid ambiguity if there are multiple classes with the same name.

Imports provide us with a way to reference classes from other packages. So if we wanted to reference something from our HelloWorld program from another class we could include something like this

package some.other.thing;

import code.to.live.by.HelloWorld;

public class SomeOtherThing {
	// ...
}

Class

Classes are a large enough topic that we will discuss them in more detail later on in a dedicated post. But, we will briefly touch on them so that we understand the basic program structure.

In our “Hello World” program we have a class named “HelloWorld.” The body of the class will contain the code needed for the class, in our case printing “Hello World” through the “main” method. Additionally, our class will live in file named HelloWorld.java

public class HelloWorld {

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

}

Main Method

The “main” method provides an “entry point” for our program and can be used to direct program flow.

It will usually look something like this:

public static void main(String[] args) {
	// ...
}

Public and static specify the “access modifiers” for the method, again something we will discuss in more detail later, but for now just know that this exposes the method so that Eclipse can our Hello World program.

String[] args specify “command line arguments” for the program. This is frankly not something I find myself using very often through Java but does allow the potential for things like command line scripting through Java.

Semicolons

Semicolons are used to end statements in Java. It is typically convention to these at the end of the line but there is still the option to “wrap” a line that has become too long.

We could potentially rewrite our line to print “Hello World” as:

System
	.out
	.println("Hello World!");

This would still be valid Java code as whitespace and newlines do not signal the end of a statement, only the semicolon.

Comments

Comments provide a way to specify lines that you do not want to be executed. The primary use case for this would be to document how a particular section of code works.

A single line comment is indicated by two backslashes //. Anything after the backslashes would not be executed. This can also be used “after” a line of code as an “in line” comment.

Comments spanning multiple lines can be specified using /* and */. Anything between the two would not be executed.

So if we were to slightly modify our Hello World program to include comments it might look something like this:

package code.to.live.by;

public class HelloWorld {

	public static void main(String[] args) {
		// This is a comment

		System.out.println("Hello World!"); // This is an in-line comment
		
		/*
		 * This is a multiline comment
		 */
	}

}

Or if we were to use comments in a way more in align with how we might see them in the future, documenting how some section of code works, it might look something like this:

package code.to.live.by;

public class HelloWorld {

	public static void main(String[] args) {
		// Print Hello World! to the Eclipse console
		System.out.println("Hello World!");
	}

}

Now you have a good understanding about basic program structure in Java

Leave a Reply

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