Classes and Object in java

Adil Wadhwania
5 min readMar 1, 2023

--

In this blog we are going to discuss what is a class and how to create objects in java

Class in java :-

  • A class is blueprint to create object in java.
  • The file name and class name should always be same.
  • A class consists of fields, constructor and methods.
  • Fields define the state of the object and methods are the actions that an object can perform.
  • The fields in the class are known as instance variable or class variables.
  • The class is template telling how an object should look, when we create an object, we ask for “object of an class”.
  • We have an Employee class that stores information of individual employee.
class Employee
{
}
  • This is an empty employee class with no fields, constructor or methods.
  • If we create an employee, we have a name for employee so let us create a field “name” in this employee class.

Field :-

class Employee
{
private string name;
}
  • We have created a field known as “name” in employee class, now the variables or fields declared inside the class and outside all the methods is known as class variables or instance variable.
  • So this variable belongs to object, as we know each employee will have different name so by declaring name as class variable we can give different names of employees while creating objects of employees.
  • Instance variables define the state of the object and whenever we create a new object of this class, the state is defined as name of employee.
  • Now I want to initialize or give value to this field name, so this is where the constructor comes in the picture.

Constructors :-

  • A constructor is special method that is called when we create an object to initialize the class variables or we can say instance variables.
 class Employee
{
private String name;

public Employee(String name)
{
this.name = name;
}
}
  • A constructor is same as the function/methods in java but without a return type.
  • The name of constructor is same as the name of class.
  • Here we have created a parameterized constructor and the parameter is string name because we want to initialize the name of employee.
  • Then the “this.name”, here “this” means current object and name means the employee name, so this defines that the current employee object’s, employee name should be whatever you passed in the constructor’s parameter.
  • Constructor can be declared private, public, default and protected as well.
  • A private constructor means it can be called within the same class only, when you don’t want to create an instance of object outside the class you can use private constructor
  • A default constructor can be use within the same package(it is nothing but similar to directory grouping same classes together), that means when we have default constructor the object of that class can be created in the same package where that class resides, and not outside that package.
  • A public constructor can be accessed from anywhere, we have declared a public constructor in our example. So we can create the object of Employee from anywhere.
  • A Protected constructor can be used in the same package and in sub classes or child classes of that class to create an object. I will explain about the subclasses in the upcoming articles.

Methods :-

  • Methods in java are similar to the functions in the C language.
  • Method is a block of code that performs a specific task and return a value.
  • Methods are typically associated with class or object and are written inside a class.
  • In this Employee examples only, let us say we want the name of an employee stored in the object, then for that we can write a getName() method.
class Employee
{
private String name;

public Employee(String name)
{
this.name = name;
}

public String getName()
{
return this.name;
}

}
  • Here we have written a getName() method to get the name of the employee, so whenever the method is called it will return that name of employee for the object through which this method is called, here “this” means current object.
  • Methods can be declared default, public, private and protected.
  • A private method is accessible or we can say use inside the class only where it is declared, public method can be use from anywhere.
  • A default method can be use in the same package where the method is written and protected method can be used in the same package and in the subclasses or child class.
  • The top-level classes(Outer classes) can be declared as public or default, public means you can access it from any other class in any package and default means you can access that class from anywhere in the same package. We can not use the private and protected with the top-level classes in java.
  • This is all about class and what we can write in class, how to use it. Now as we know class is blueprint to create an object so let us see what is an object.

Object in Java :-

  • Object is instance of a class, it represents an entity such as person, employee, bank account or a car etc.
  • Whenever we create an object of a class then the constructor is called of that class. An object is created using new keyword in java.
public static void main(String[]args)
{
// syntax to create an object in java
Employee employee=new Employee("Adil")
}
  • We are continuing the example of Employee created while understanding the class, In that we have a constructor that takes String as input. So here we have created an object of Employee using new keyword and passed the name of employee. This is how objects are created.
  • Each object in java holds its own state or we can say values and that is defined based on the instance variable, because we can create multiple Employee objects with different names.
  • If we do not create an constructor in the class it will call the default constructor that has no parameter. Ex :- Employee e=new Employee();
  • You can call the method of the Employee class using the employee object you have created and the accessibility of the method can vary based on the access modifier(public,private..) it is declared with and from where you are accessing it.
public static void main(String[]args)
{
Employee employee=new Employee("Adil")
employee.getName();//output : Adil

Employee employee1=new Employee("Ramesh")
employee.getName();//output : Ramesh
}
  • Here I had called getName() method that is public, accessible from anywhere by using the “employee” object and the output is Adil. And the output of “employee1” object would be the name of employee1 that is Ramesh.

“Thank you for taking the time to read my article! I hope you found the information and insights shared here helpful and interesting. If you enjoyed this article, be sure to follow my article for more similar content. I’m always exploring new topics and sharing my thoughts and insights on a variety of subjects. I appreciate your support and look forward to connecting with you through my writing. Until next time!”

ADIL WADHWANIA :)

Topics :- java, java class, java object, java for beginners, java tutorial, java constructor, java instance variable, java methods, java programming

--

--

Adil Wadhwania
Adil Wadhwania

Written by Adil Wadhwania

Tech-savvy writer sharing insights on programming,current affairs,sports,great personalities, and new technologies. Follow for engaging and informative content.