Learn classes and objects in java

Hey guys!! Welcome to flower brackets blog. Today let’s learn classes and objects in java.

A class is basically a data structure where you can define variables which are called member variables and methods which are called member methods.

Class has got an important property, that is, you can create instance or object from class.

So you can maintain different states of class using instances or objects. Every java project has at least one class and one main method.

// class employee
public class Employee
{ 
   int id;
   String name;
   int age;
}

public class ClassDemo
{
   public static void main(String[] args)
   {
      Employee john = new Employee(); // john is an object or an instance
      john.id = 05;
      john.name = "John Thames";
      john.age = 14;
      System.out.println(john.name + " is " + john.age + " years old.");
      Employee sam = new Employee(); // sam is object or instance
      sam.id = 06;
      sam.name = "Sam joseph";
      sam.age = 15;
      System.out.println(sam.name + " is " + sam.age + " years old.");
   }
}

Output:

Learn Classes And Objects In Java

We have created Employee class. This class can have member variables and methods.

Employee class have an “id” of type integer , Employee name of type string and Employee age of type integer.

This class can also have member methods that we will see in a moment.

Also read – best way to reverse string array in java

Now to call Employee class in main method we have to create an instance of the class.

So, to do that call class Employee give a name to its instance, john. We have created an instance/object of Employee class which we named as john.

Now this object is used to assign values to the member variables of the Employee class as in the above example.

In similar way we can create different instances of Employee class. So using a class we can define object which are independent of each other.

So as in the above example “john” instance values are independent of “sam” instance values.

This is very important property of object that it can store its own state and it’s independent of the other instance.

Now in java programming direct usage of variables are discouraged.

john.id = 05;
john.name = "John Thames";
john.age = 14;

Because any programmer can modify these values. So we use methods which can get or set these values. What actually these getting and setting values is,

// for example in employee class
public class Employee
{
   int id;
   String name;
   int age;
 
   public int getId()
   {
      return id;
   }

   public void setId(int id)
   {
      this.id = id;
   }

   public String getName()
   {
      return name;
   }

   public void setName(String name)
   {
      this.name = name;
   }

   public int getAge()
   {
      return age;
   }

   public void setAge(int age)
   {
      this.age = age;
   }
}

So, whenever we want to assign some value to the “id” we use “setId” method and whenever we want to get the value of “id” we will use “getId” method and this is called encapsulation of a variable.

This is important because we do not want to give access to variables to some other users which are unknown to us. Now to assign value to “john” and “sam” instance we use below format,

public class SampleExample
{
   public static void main(String[] args)
   {
      Employee john = new Employee(); 
      john.setId(05);
      john.setName("John Thames");
      john.setAge(14);
 
      Employee sam = new Employee(); 
      sam.setId(06);
      sam.setName("Sam joseph");
      sam.setAge(15);
 
      System.out.println(john.getName() + " is " + john.getAge() + " years old.");
      System.out.println(sam.getName() + " is " + sam.getAge() + " years old.");
   }
}

Output:

John Thames is 14 years old.
Sam joseph is 15 years old.

When we run the program result will be same but this is more preferred when you are accessing or assigning values to the member variables.

So this is how classes works in java.

Related Posts