Classes and Objects

In Object Oriented Programming Language, Classes and Objects are building blocks. An Object is an instance of a Class, and Class is a template containing attributes and behaviors of similar type of objects. In other words, a Class is a common noun, like Employee, and Object is a proper noun, like John Doe.

Employee John Doe has a name, an ID, address etc., similarly Jane Doe also has a name, an ID, address. Similarly John and Jane both perform some activity based on their designation.

In Object Oriented Programming, information about an object can be represented in the form of instance variables, they signify the state of the object. The activities that object perform are represented by methods.

We collate all instance variables and methods which are common across multiple objects and put them together in a class. We can then use this class like a data type to create objects out of it.

package com.techstackjournal.app.types;

public class Employee {

	public String name;
	public String id;
	public String city;
	
	public String toString() {
		return "Employee [id=" + id + ", name=" + name + ", city=" + city + "]";
	}

}

Above is a very basic Employee class, it has three instance variables (name, id, city) and one method (toString).

All of the instance variables in this program are of type String. String is not a primitive type, it is a built-in class provided by Java. It can be used to store set of characters together in a single variable which is not possible in char type. We will discuss more about String later.

Using methods we can perform operations on the data. Methods can take data as input and return data as output. In this program, we have toString method which returns values of each instance variable to the calling statement. We will discuss more about methods later.

All the instance variables and methods are declared as public, so that I can access them via reference variable of an object. This public keyword is an access specifier. There are other access specifiers too which we will learn later.

Employee class is just a template, we do not have objects yet. Let us see how we can leverage this class and create objects.

package com.techstackjournal.app.main;

import com.techstackjournal.app.types.Employee;

public class Application {

	public static void main(String[] args) {
		
		Employee emp1, emp2;
		
		emp1 = new Employee();
		emp1.name = "John";
		emp1.id = "E001";
		emp1.city = "New York";

		emp2 = new Employee();
		emp2.name = "Jane";
		emp2.id = "E002";
		emp2.city = "New York";

		System.out.println(emp1);
		System.out.println(emp2);

	}

}
Employee [id=E001, name=John, city=New York]
Employee [id=E002, name=Jane, city=New York]

A class called Application is created to run the program. Application class is defined within a separate package other than Employee class.

To use any Java class which is not present within the same package, you need to import it using import keyword. JVM will load that class and make it available for your program during the execution.

Variables emp1 and emp2 are just reference variables. That means, these variables can hold the memory location of Employee object. A reference variable without object will be pointing to null..

Two Employee objects are created using new keyword. This is when Java allocate memory to store values in these objects.

Once object is created, you can access its instance variables and methods using dot operator on reference variable, like emp1.name etc.,

If you try to access the instance variable or method on reference variable without creating the object using new keyword, you will get a NullPointerException. What does that mean is, the reference variable is pointing to null, but an attempt is made to refer an instance variable violating the semantic constraints of the programming language.

Sometimes programmers also call instance variables as member variables, methods as member functions, and both of them collectively as members.

Did you notice one thing?

Variables name, id and city are of String type which is a class. We learned that objects are created with new keyword. But we didn’t used new keyword when we initialized them. It is an exceptional case for String objects, though Java still accepts new String(text) syntax too. We learn more about String class later.

Points to Remember

  • Class is a template or blueprint of similar kind of objects. It describe the state and behavior
  • State in a class is represented by instance variables
  • Behavior of class is represented by methods
  • Object is an instance of class
  • Object can be created using new keyword followed by the class name of which you want to create an object
  • Each object of a class will have its own copy of instance variables
  • Object can access instance variables and methods using dot operator
  • Attempting to access instance variables or methods without creating object will result in a NullPointerException
  • We use import keyword to load required classes in our program
  • Instance variables and methods can be declared as public