Archangel Macsika Sikademy Image
Archangel Macsika

Learn Syntax of Objects, Classes & Methods in Java Programming

Tutorial 4 of 9 | 6 minutes read
Learn Syntax of Objects, Classes & Methods in Java Programming - Sikademy

In the last tutorial, we wrote our first java program saved in the file "". In this tutorial, you will learn about the basic java syntax using the program from the previous tutorial. Before that, you should know that a typical Java program can be defined as a group of objects that interact by calling each other's methods. It's okay if this definition seems confusing at first, we will explain some basic key terms that will help you understand the Java programming language better.

Understanding Objects, Classes, and Methods in Java


A class can be defined as a template/blueprint that describes the state and behavior of the objects created from it.


An object is an instance of a class.


Methods define the different behaviors of a class. Each method is intended to describe the specific behavior of that class.

Class, Objects & Methods in the context of a vehicle

To properly understand classes, objects, and methods, let's analyze a vehicle. There are different kinds of vehicles such as cars, buses, motorbikes, etc. They all have similar characteristics such as movement and navigation as well as properties such as wheels, steering, dashboard, tires, etc. Here, "vehicle" is a class while cars, buses, and motorbikes are all objects of the class "vehicle." The methods are the various actions it performs such as move forward, move backward, turn left or right, brake, etc.

Interestingly, we can even narrow the example above, and view a car as a class of its own.

There are different kinds of cars such as SUVs, Sports Cars, Convertibles, etc. Each type of car has similar characteristics such as movement and navigation as well as properties such as wheels, steering, dashboard, tires, etc.

If you still find this confusing, just know that an object has an identity, which means each object is a distinct entity. An object has a state, which are the various properties that describe it and is subject to change. An object has a behavior, which means it can do things and can have things done to it. Each behavior is seen as a method of that class in which the object was created.

We will talk more about this in the intermediate and advanced tutorial series. For now, just know the basics.

A typical Java program — "Hello World" Example Explained

To run the example below on your computer, we assume you have properly installed Java, otherwise go to the previous tutorial:

Save the program above using, open Command Prompt (for windows) or Terminal (for Mac and Linux). Compile the program using the command javac

If there no errors encountered, the Command Prompt (for windows) or Terminal (for Mac and Linux) will take you to the next line. Next, type java TutorialClass to execute the program.

Note: Always Save and Compile your Code before executing, otherwise, you will not get the desired output.

Your output should read:

Hello World Cheers! From Sikademy.

Let's look into each line of code

The first line of code encloses the program inside a class using curly braces { ... }. Every line of code that runs in Java must be inside a class. In our example, we named the class TutorialClass.

public class TutorialClass 

The second line is a single-line comment that explains what the program does. It is not mandatory to add comments to programs, but it is essential for code readability.

// This is my first java program. This will print 'Hello World Cheers! From Sikademy.' as the output

The third line is known as a method. This is a main() method in java. Every Java program must have a main() method. This is because, when you run a Java program, the execution of the program starts from the main() method. As a result, main is a reserved keyword in Java. Methods also enclose their content in curly braces. Any code inside the main() method will be executed. For now, you don't have to understand the keywords before and after main method. You will get to know them one after the other in this tutorial series.

public static void main(String[] args)

The fourth line uses the println() method within the main() method to print a line of text to the screen.

System.out.println("Hello World\n\nCheers! From Sikademy.");

When writing programs in Java, it is important to keep these tips in mind:

  • The Name of the program file should be the same as the name of the class. When saving the file, save it using the class name and append '.java' file extension to the end of the name.

    Note: Matching class name and file name is required when a public class is used. Every Java program must have a public class that holds the main() method and there can only be one public class in the entire program. However, you can have other public classes denoted as public ClassName. Yow will learn everything about classes in intermediate and advanced tutorials.

  • Java is case sensitive, which means the words "Baby" and "baby" may mean different things when used as identifiers.

  • The first letter of all class names should be a capital letter. If several words are combined to form the name of a class, the first letter of each word should be in upper case. e.g NameOfMyClass, and TutorialClass.

  • The names of all methods should be in lower cases especially the first letter. However, if several words are combined to form the name of a method, the first word should be in lower case, while the first letter only of each word that follows should be in upper case. This is called camel casing in programming. e.g nameOfMyMethod.

  • In Java, each code statement must end with a semicolon.

Tutorial Exercise

Write a program with the class name "LearningMaterials" and use the "main()" method and println() method to print five different learning materials.

Enjoy this Tutorial? Please don't forget to share.