
In my other tutorials, I have included code examples without much explanation as to what each piece of code is or why it is formatted the way that it is. Here, I’m going to explain the structure of a Java program so that you can better understand any code examples that I provide.
Below is an example of a Java class.
package com.example;
class ButterRobot {
int timesButterPassed = 48981;
int totalWeightOfButterPassed = 105367; // weight in ounces
void passButter(int weightOfButter) {
timesButterPassed++;
totalWeightOfButterPassed += weightOfButter;
}
boolean isPurposeNotToPassButter() {
return false;
}
}
Code Blocks
First, let’s take a look at what we call “blocks“. The start of a block is indicated by { and the end is indicated by }. Anything between these is considered to be inside the block. Blocks are used for many different things in Java as you can see above. It is not only used to define the class but also each method and logic statement as well.
Classes

The class definition is similar to a function except a class does not accept parameters and the naming convention is Upper Came Case where every word is capitalized.
class ButterRobot {
...
}
The main purpose of a class is to organize functions into coherent “things” so that we can wrap our head around what’s happening much easier. Here, our “thing” is a butter robot.
Packages
In the same way that classes are used to organize functions, packages are used to organize classes.
package com.example;
The standard package definition is a backwards domain name. For this package definition, the domain name would be http://www.example.com.
Functions
A function is a piece of code that performs a certain task. You can think of a function similar to a button on a remote for your TV. In the same way that each button you press on a remote does something different to the TV, each function does something different to your application. In the example above, we have two functions:
void passButter(int weightOfButter) {
...
}
boolean isPurposeNotToPassButter() {
...
}
You may have noticed that the functions live inside the class block. When a function is inside a class, it is known as a method that belongs to that class.
Here, the void keyword tells Java that the function is not asking for an answer but is simply doing something. The keyword boolean on the second function tells Java that the function is asking for a yes or no response. This is called the return type of the function and must be present before the function name.
The function names (vendItem and stockItems) are arbitrary names that briefly describe what the function does. Notice that the first letter in the name is lower-case and any words following the first word start with a capital letter. This is known as Lower Camel Case. Function names in Java should always follow this convention.
The last thing to note about functions is that the name declaration must be followed with a set of open-and-close parentheses ( ). However, in the second function, notice that we are taking in an int (integer) called weightOfButter. This is known as a parameter. Parameters can only be used inside the function to which they belong. That is, they can only be used inside the curly braces { } that come after the parentheses ( ).
Variables
Variables as they exist in java can be in one of two forms. They can either be field variables or parameter variables. Field variables exist throughout an entire class and parameter variables exist only in a single function. In the code example here, we have two int fields:
int timesButterPassed = 48981;
int timesAnythingElseHappened = 0;
and one int parameter (weightOfButter):
void passButter(int weightOfButter) {
timesButterPassed++;
totalWeightOfButterPassed += weightOfButter;
}
Notice how both fields and the parameter can be used inside the passButter method. If we tried to use weightOfButter inside the isPurposeNotToPassButter method,
boolean isPurposeNotToPassButter() {
weightOfButter++; // This is bad!!
return false;
}
our IDE would give us a compilation error message “Cannot resolve symbol ‘weightOfButter’ “. That means that our program would not run because it does not know what to do with weightOfButter inside that method.
Other Notes
Keep in mind that the purpose of this article is to show the structure of a java program and not to explain every word that you may see inside a java file. In later tutorials, I will explain all of the different keywords, operators, class types, static, etc.