Java Code Structures
Every Java application projects are just collection of Java files ( aka Class files ). Understanding the structure of Java file is crucial for understanding Java programming.
In this module, we will cover following topics:
- Anatomy of Java File
- Class Declaration
- Method
- Executable Code
› Anatomy of Java File¶
A file with .java extension is a code file where we write our Java code (instructions to computer ). You will create this code file in the src/main/java folder on your project. The java code in this file is human-readable and will be compiled into machine code when we turn our project into a program.
Java source file contains following items:
| Components | Purpose |
|---|---|
Class Declaration |
Defines region of code that belongs to the Java file |
Data (Field) |
List of data available in the class |
Methods |
Contains executable code that achieves specific function |
Let's look at the components of the Demo.java file:
These components are present in almost all Java files. So, let's understand them with more details and explanations.
› Class Declaration¶
Every Java file must contain class declaration. It begins with keyword class followed by the java file name and code block { } that indicates the start and end of the code region.
{ and } belongs to the Java file. Therefore, if you write any java code outside the class code blocks, these code will be treated as invalid, resulting in a programming error.
Again, it is important to remember that the Java file and class declaration names must be identical. Otherwise, it will also result in a programming error.
✔️ Java file name and Class declaration name are identical.
❌ Java file name and Class declaration name are different.
› Method¶
A method is a container for a group of useful executable code. As an analogy, the button on your TV remote controller is similar to a method. It groups and hides complicated engineering details as a mere button.
Using just a method name, we can execute often-repeated many lines of code. Some fantastic benefits of the method are:
- assigning a descriptive name to a group of organized code
- reduction on repeated lines of code
- reduction in development complexity
- update on code happen on a single location
- we can re-use the method on another programming project
From our example Demo.java file, we can observe the following two methods:
- the method name is the main
- it groups and hides the highlighted executable code
Remember that you can create as many non-identical methods in a given Java file. So if you find yourself using the same lines of executable code here and there in your project, it's time to group these lines of code into a method and store it in some Java file (class) for reuse.
› Executable Code¶
In Java, a single line of code directly executable to the CPU is called statements. Whether a single statement or multiple statements, it achieves a meaningful task when executed.
Single Statement
When executed, this single statement instructs CPU to print a message "HELLO TESTER!" to the terminal.
Multiple Statement
When executed, this multiple statement instructs CPU to take a screenshot from a browser.
As we went over from previous section, its a good idea to group multiple statements into a method. So that we can use these code by a single method name and reuse it elsewhere in the project.
As you can see, whether single or multiple, each statement must end with a semicolon ; to indicate the end of the instruction to the CPU.