Anatomy of a Java Class

Pasan Kamburugamuwa
3 min readOct 11, 2021

--

As a java developer, you must know the basics of Java programming before solving big problems. So let’s go through some basics of Java.

You can think of a class as like a blueprint. For example, the class House below is a blueprint of a house and the objects are individual houses built from that blueprint. You must have the blueprint as the class which allows you to describe the house.

So given below is a simple example of how to define your own java class. After that, you need to create objects

public class House
{
// define the class in here

}
//create the object myHouse using House blueprint.
House myHouse = new House();
//create second object neighborsHouse using House blueprint
House neighborsHouse = new House();

You need to remember that the objects have attributes and behaviors. These correspond to instance variables and methods in the class definition. Instance variables hold the data for objects and methods in that class, define the behavior. Constructor is used to initializing the variables when the object is created.

Let’s go with how the Java program runs in the machine. When the JVM starts running, it looks for the class you give it at the command line. Then it starts looking for a specially-written method that looks exactly like,

public static void main(String[] args) {
//your code in here.
}

JVM runs everything between the curly braces{} of your main method. In every java application, you need to have at least one main method.

The below diagram shows, how the Java program is defined.

The diagram is taken from Head First Java by Kathy Sierra

In java, everything goes in a class. After you type the source code file( with .java extension), then compile it into a new class file (with .class extension). You run your program means, you are really running a class.

Running a program means telling the Java Virtual Machine (JVM) to “Load the Hello class, then start executing its main () method. Keep running ’til all the code in main is finished.”. No matter, how big is your Java application, there must be a main() method, in order to run the application.

What should be inside the main() method in Java application?

Inside the main() method, you can have anything that the java program should run. Loops, branches, statements or anything that you want to build your application can be included.

Some of the basic questions you might have regarding the java application are given below.

  1. Why does everything have to be in a class?

Nearly everything in java is an object and so because of its object-oriented nature, everything in java is a class.

2. Do you need to have the main method in every class in java?

No, you only need to have one main method, the main method is used to start the java program.

3. What is the difference between System.out.println and System.out.print?

System.out.println inserts a newline and System.out.print keeps printing to the same line.

So this is all about the anatomy of java classes. Hope you understand most of the things in here and will discuss most of the things related to JVM, classloader, compiler in a separate tutorial. Hope you understand, what I am trying to explain and see you in the next tutorial. Until then, Bye.

--

--