Java Classes and Objects

Classes and Objects are at the heart of every Object-oriented programming language and java is not an exception.

What is a class? Class is a template or blueprint that describes the kinds of state and behavior that objects of its type support.
What is an object? An object is an instance of a class and each object will have its state and access to all of the behaviors defined by the class.

Now, you must be wondering what is this state and behavior we are talking about here, right? Well, don’t worry, they are not that scary 😛

State – State are the instance variables and each object will have its own unique set of instance variables as defined in the class.
Behavior – Behaviour is also known as methods. Well, if you know about any of the programming languages ( like C or C++ ), then most probably you already knew about the methods or we can say functions. Methods are where the class logic is stored, the algorithm gets executed, etc.

How to declare a Class?

We have to learn here that the class defines a new data type and once defined, then this data type can be used to make objects. When we define the class, we normally declare its exact form and nature. We can do this by specifying the data and the code on which it operates, though the classes, can contain only the data or the code part, while most of the real-world classes contain both.

A class is declared by the class keyword. A sample class declaration is shown below

class classname {
	type instance-variable // "state" or "instance variable"

	// "behavior" or "methods"
	type methodname(paramters){
		// body of the method
	}

}

The data or variables, defined within the class are called the instance variables and the code is contained within the methods. Together they are called the members of the class.

Types of variables in a class

  • Local variables – These variables have a life expectancy within a block or method and cease to exist after the block or method ends.
  • Instance variables – Instance variables are defined within the class but outside of a method and thus each object will have a copy of the instance variables.
  • Class variables – Class variables though defined within a class but they are shared among all of the objects of the class and thus there will only be one copy of these variables. They are also called static variables.
class Codekru {

	int instanceVariable; // this is an instance variable

	static int classVariable; // this is a class variable

	public void method() {
		int localVariable; // this is a local variable
	}

}

How to make Objects of a Class?

Let’s create a class first, we named our class Codekru, you can make a class of your liking 😛

class Codekru {

	int i;
	int j;
	int k;

}

Objects are the instances of a class and they can be various ways to create an object, most common and easy one is by using the new operator.

Now creating objects is mainly a two-step process –

  • First, we have to declare a variable of the class type, this variable yet does not define an object, it is simply a variable that can refer to an object.
  • Second, we must acquire an actual physical copy of an object and assign it to the variable, we can do this by using the new operator.
Codekru codekruObject;  // declaring reference to Object 
codekruObject = new Codekru();  // actually allocated the physical space here 

1st statement just declares the codekruObject as a reference to an object of type Codekru. After this statement, codekruObject will have the null value, which indicates that it does not yet point to an actual object. Any try of accessing the data members of the class will result in an error at this point, but after using the new keyword, codekruObject will be allocated a space and thus we will be able to use its data members

Above two lines of code can also be written in one line as shown below

Codekru codekruObject =  new Codekru();

Now, we have a basic understanding of Classes and objects, so let’s take things a little further and make them a little interesting. How, you ask?

We are going to first create a class with variables only ( just to keep things simple)

class Codekru{
   int i; 
   int j;
}

Now, let’s make two objects as shown below

Codekru object1 = new Codekru();
object1.i = 20;
Codekru object2 = object1;
object2.i = 40;

Now, the question is what would be the value of object1.i ?

Well, the answer is 40. Yes, you read that right, the answer isn’t 20 but 40, but why so? because we never created a new object for object2 or allocated a new space for it (as we never used new), it was referring to the location of object1, so both object1 and object2 would end up referring to the same location.

referring to same object

So, changing any data members using either of the objects will affect the other object.

Hope you liked the article. If you have any doubts or concerns, please feel free to reach us in the comments or mail us at admin@codekru.com.

Liked the article? Share this on

Leave a Comment

Your email address will not be published. Required fields are marked *