Program Structure

What's a Java program like?

Basic Java Program

Let's look at a basic Java program and break it down.

MyProgram.java

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

This program will print "Hello World" to the console. In almost every Java program including this one, you will notice a few things.

Filename

The filename of every file in a Java program must be the name of the single public class, interface, or enum.

There can only be one main public class, interface, or enum in a file.

Java Syntax

Notice how each block of code begins and ends with curly braces. Curly braces show Java what part of your program belongs with which block of code.

Also, each code statement must end with a semicolon.

Comments

When we write programs in any language, we want to be able to understand what specific parts of our code does. We can write comments in our code to give ourselves and other people an idea of what this code does.

There are two ways of writing comments in Java:

/**
* multi-line comment
*/
// single line comment

Multi-line comments must start with /* and end with */

Classes

Our Java program starts with the following line: public class MyProgram. Java is considered to be an "object oriented language". What this means is that all code that will be run by our program must be inside of a class. Everything in Java revolves around objects and classes. Objects are an instance of a class, and each object contains its own instance state.

We learn more about classes in another lesson.

Methods

Methods, or functions, are a set of commands that are used perform specific actions. Within our class MyProgram we have a method titled main. This method contains a command that calls the method println. Methods help us to be able to reuse code, so that we can write it once and then call it again anywhere else in our code.

Jump to our lesson on methods.

Access Specifiers

In Java, not everything in one class or program is openly available to another. This is called access specification. A programmer can specify the access of their program and it's components by using public and private. Public means that any other program can use this class, variable, or method; private means that only this program can use this class, variable, or method. There is a third access specifier (protected) but we won't use it in this course.

Later, we will explain in more detail the importance of access specification.

Data Types

In this program, you will notice the usage of void and String. These are Java data types, and they tell Java what type of data a specific variable or value will be. In this case, stating void before a method tells Java not to expect a value to be returned from this method (this is a return type), and using String[] tells Java that the following variable will be an array of Strings.

Learn more about data types in the variables lesson.

Quiz

Which code segment is correct?

#1
#2
#3
#4
Answer
#1
public class Hello {
public void World()
System.out.println("!");
}
#2
public class Hello {
public void World() {
System.out.println("!");
}
}
#3
public class Hello {
public World() {
System.out.println("!");
}
}
#4
public class Hello {
public void World() {
System.out.println("!")
}
}
Answer

2 is the correct response. Notice that each code block is wrapped in curly braces, the method World has a void return type, and that the code statement has a semicolon after it.