An introduction to Java Class and Object

In this tutorial, we will introduce Java class and object. By now you must have heard that Java is an object-oriented programming language and everything in java is an object backed by a template called a class.The aim of this tutorial is to introduce new programmers to classes and objects and not to explain the workings of a JVM.

Why do we need Java classes?

Let’s first try and understand the need for classes. When you are performing an operation such as addition on a calculator, you tell the calculator to take two numbers and return their sum. That’s simple. However, in a real world, you probably have more complicated problems to solve. Problems that have multiple steps and multiple entities involved. Let’s say that now instead of adding two numbers you want to write a piece of software that generates an invoice for a restaurant. The problem is not as simple as adding up the cost of each dish because we are creating a state of the art restaurant where the ordering happens via a hand-held device and as soon as a customer enters, the software starts tracking his orders.

This task is probably too difficult for a calculator (not adding the price of each dish, but maintaining what dishes a customer has ordered, extra toppings, taxes etc). To design this system it would be easier to think of what entities are involved in this scenario. We have a customer, a dish and a waiter (so that he gets the right tip). Each entity has certain information stored in and can perform certain actions. So a customer can order a dish, return a dish, ask for a bill, pay money and so on. The dish can have an amount, possible extra toppings, and variations. A waiter needs to be assigned to a customer. It looks like we need to be able to define an entity (customer), define what actions it can perform (order dish) and also any useful information about the customer(name, address, phone number). The object-oriented paradigm gives a method to define such entities.

The definition of a java class:

We define Class as a template that stores information about the state(customer’s name, address etc) and operations that an entity can perform (ordering a dish). If we were to write a class definition in English then the class of a customer would look like

Class Customer :
a name, address and phone number
order a dish, be assigned a waiter, be given a bill

In Java this is how you would define a class

public class Customer {

   private String name;
   private String address;
   private int age;

   public Dish orderDish() {
     //return a dish
   public void assignWaiter(Waiter waiter) {

    // assign the waiter


Don’t worry about the syntax for now but there a couple of points that you should notice. Every entity has a class definition. So when you assign a waiter, the waiter needs to have its own class definition. There are some primitives that are not classes (int, double) etc. These are so basic that they can’t store any other state besides their own and can perform no operations. Besides the primitives, though, everything else in java has a class definition.

What are Objects then?

We defined classes in the previous example. Objects are the actual implementations of classes. So a customer is a class, but when a new customer walks him we create a new object from the class. I will reuse the blueprint example since its so easy to understand. If you are a building developer and are planning to create 50 house units and sell them, then the blueprint that you use to create the houses is called a class and the actual house that you create is an object. An object occupies memory each object occupies a different area of memory even if they are built from the same class. Just as each house is built on its own piece of land even if the houses are all built from the same blueprint.

Do you need more help to understand classes and objects?

Think of as many real time scenarios as you can and then think about how the ‘actors’ or ‘entities’ in that scenario interact. Understanding object-oriented concepts is crucial since all operations in Java are object-based (unlike in some programs where they are not). Whenever you think of an information or an operation think of ‘what entity’ can hold that information or perform that operation. When you start coding, start by defining the entities (classes) and then create objects out of those classes to start storing information or processing information.

This tutorial should have helped you to understand what classes and objects are. If you are new to this concept then sleep over it for a day before going to the next concept, since its absolutely essential for you to be crystal clear about it.

Leave a Comment