Classes and Objects in Java

What is the Class?

The class is a logical construct on which the entire Java stands, as it defines the overall state and behavior of an object. Any feature that we expect to be available in a program needs to be encapsulated inside the class.

Till now, we have seen the use of class in our examples, and also Wrapper class along with their utility was introduced. It was rudimentary and was available for the purpose of encapsulating the main method. Classes can be seen as a data type, which once defined acts as a template. The class is a logical entity. The object can be created using this template and used throughout. The object so created is a physical entity analogous to the class.

What is an Object?

The Object is an instance of the class. The template so defined comprising of the state and behavious has its physical form as an Object.

Let us take a real-life example. We often see the construction of buildings. These constructions do not happen unplanned. There is a blueprint that contains every intricate detail of the building. Making a plan of 10,000 sq feet complex does not allocate this much space. The building so generated according to the blueprint is the one which takes space. So blueprint is the class, and the building is the object. We can create so many instances, objects using the same blueprint.

General form of class

The class is declared by using the keyword class. When we define it, we are specifying the data that is expected to be contained, and also the code/instructions that operate on the data. This code was previously referred to as the behavior.  The general definition of the class is as follows:

class <className>
{             
	  /*data*/
	 <dataType1> <variableName1>;
	 <dataType2> <variableName2>;
	 //.. and so on

	 <dataTypeN> <variableNameN>;

	 /*behaviour*/
	 returnType <methodName1>(parameter-List){
		//block of code
	 }

	 returnType <methodName2>(parameter-List){
		//block of code
	 }

	 //..and so on

	 returnType <methodNameN>(parameter-List){
		//block of code
     }
}      

The data or variable defined inside the class without the keyword static are called instance variables. The block of code is a part of the behavior that we are going to address as a method hereon. Collectively, the variables and methods within the class are called members of the class. Every object so created will have its own unique copy of the variable.

One thing to be noted, a class not necessarily need to contain both the data and method. A class can exist with only data or only methods. But that is rarely the case. We often need both to be encapsulated to utilize the essence of OOP.

Let us start with our first class.

class TechieCluesMembers{
	String memberName;
	String email;
	String password;
}

We have defined a class that keeps the data of our members. It comprises of data like memberName, email, and password. Now hereon,  TechieCluesMembers is a data type. A template has been created, not the actual object.

In order to create an object of our own data type, we use are going to use the new keyword. As indicated earlier too, the new keyword calls the constructor(We will be discussing Constructors in our upcoming lecture) of the class.

TechieCluesMembers techie1 = new TechieCluesMembers();

A new object of type TechieCluesMembers has been created and the reference of this object is stored in the variable techie1. This techie1 object has a physical existence in memory.

In order to access the members of a class, we use the (.) operator. The format is objectName.memberName. For example, to access the variable memberName, we use techie1.memberName.

So statement as techie1.memberName = "Jammy" asks the compiler to assign value Jammy to the member variable memberName of the object techie1.

Let us look into a few examples that help to give a clearer vision to Objects :

class TechieCluesMembers{ //Our class which we defined earlier
	String memberName;
	String email;
	String password;
}

public class ClassWithMain{ //This is our driver class
	public static void main(String args[]){ // This is method main . The starting point of program
	TechieCluesMembers techie1 = new TechieCluesMembers(); //Instance 1
	TechieCluesMembers techie2= new TechieCluesMembers(); //Instance 2

	//The following assignment affects only techie1. It has no correlation to techie2 as the members declared here are instance variable.
	techie1.memberName="Raven";
	techie1.email="[email protected]";
	techie1.password="Raven@1";

	//The following assignment affects only techie2. It has no correlation to techie1 as the members declared here are instance variable.
	techie2.memberName="Sherli";
	techie2.email="[email protected]";
	techie2.password="Sherli@1";

	//Time to print them and see what is the overall impact
	System.out.println("MemberName of techie1 :"+ techie1.memberName);
	System.out.println("E-mail of techie1 :"+ techie1.email);
	System.out.println("Password of techie1 :"+ techie1.password);
	System.out.println();
	System.out.println("MemberName of techie2 :"+ techie2.memberName);
	System.out.println("E-mail of techie2 :"+ techie2.email);
	System.out.println("Password of techie2 :"+ techie2.password);
	}
}

Output:

MemberName of techie1 :Raven
E-mail of techie1 :[email protected]
Password of techie1 :Raven@1

MemberName of techie2 :Sherli
E-mail of techie2 :[email protected]
Password of techie2 :Sherli@1

This was an overview of Java Class. In our next lecture, we will look deep into the members of Java.