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 "TutorialClass.java". 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 interacts by calling each other's methods. It's okay if this definition seem confusing at first, we will explain some basic key terms that will help you understand Java programming language better.

Understanding Objects, Classes, and Methods in Java

Class

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

Object

An object is an instance of a class.

Method

Methods define the different behaviors of a class. Each method is intended to describe a 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, dash board, tires etc. Here, "vehicle" is a class while car, 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, Sport Cars, Convertibles etc. Each type of car has similar characteristics such as movement and navigation as well as properties such as wheels, steering, dash board, tires etc.

If you still find this confusing, just know that an object has identity, which means each object is a distinct entity. An object has state, which are the various properties that describes 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 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 TutorialClass.java, open Command Prompt (for windows) or Terminal (for mac and Linux). Compile the program using the command javac TutorialClass.java.

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 Java program, the execution of the program starts from the main() method. As a result, main is a reserved key word 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 and file name is required when a public class is used. Every Java program must have a public class which 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 method 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.