Home >>Java Tutorial >Object and Classe in Java

Object and Classe in Java

Object and Classe in Java

In this tutorial, we will learn about the objects and classes in Java as we know as a fact that Java is an object-oriented programming language and in object-oriented programming technique, everything program is designed using the objects and classes.

Along with the attributes and methods, every single thing in Java is basically associated with classes and objects. For instance: A book is an object in real life. The book has attributes, like weight and color, and methods, like pages and index. A Class is somehow like an object constructor, or we can say "blueprint" that is used for the creation of the objects. In simple words, it can be understood that an object in Java is basically the physical as well as a logical entity, on the other hand, a class in Java is only a logical entity.

Object in Java

An Object is basically an entity that possess state and behaviour for instance, pen, chair, bike, marker, car, table, etc.. An object in Java can be physical or logical (tangible and intangible).

An object generally have three major characteristics that are depicted below:

  • State : This characteristic represents the data or the value of an object.
  • Behaviour : This characteristic represents the behaviour or the functionality of an object like knowledgeable, lengthy etc.
  • Identity : This characteristic of an object identity is basically implemented via a unique ID and the catch here is that the value of the ID is generally not visible to the external user. But, in order to identify each object uniquely, it is used internally by the JVM.

Let's understand it with an example, Book is an object. It has a name that is Mathematics; color is black that is known as its state. It is used to study the tables hence, reading is its behavior.

An object in Java is basically an instance of a class and a class is basically a template or blueprint from which the creation of objects happens. Hence, it can be said that an object is the instance (result) of a class. The simple definition of an object in Java can be: an object is known to be a real-world entity, runtime entity, an entity that has state and behaviour, and an instance of a class.

Create an Object in Java

An object in Java is basically created from a class. First the programmer have to create the class named MyClass, then they can use this class to create objects. In order to create an object of MyClass, The programmer have to specify the class name that will be followed by the object name and lastly use the keyword new.

Here is an example that is depicting the process of creating the object in Java:

public class DemoClass 
{
  int x = 10;
  public static void main(String[] args) 
  {
    MyClass Obj = new DemoClass();
    System.out.println(Obj.x);
  }
}

Class in Java

A class in Java is basically a group of objects that possess common properties. It can also be said that a class is a template or blueprint that is used for the creation of objects. As class in Java is a logical entity, hence; it can't be physical.

A class in Java generally consists of the functions or elements that are depicted below:

  • Blocks
  • Fields
  • Methods
  • Nested class and interface
  • Constructors

Create a Class in Java

In order to create a class in Java the programmer have to use the keyword class.

Here is an example that is depicting the process of creating the class in Java:

public class DemoClass 
{
  int x = 10;
}

Instance variable in Java

Instance variable in Java is a variable that is generally created inside the class but outside of the method. The memory is not occupied or allocated by the Instance variable in Java at the time of compiling. The memory is occupied or allocated to the instance variable at the runtime when an object or instance is created hence, it is called as instance variable.

for Ex
 int x = 10;

Method in Java

A method in Java language is basically like a function that is used to expose the behaviour of an object.

Advantage of Method

  • Code Reusability
  • Code Optimization

new keyword in Java

In order to allocate the memory at the runtime, the new keyword is used in Java. Heap memory area is the area that is where all the objects gets the memory.

Object and Class Example: main within the class

In the following mentioned example, a Student class is created that basically has two data members id and name. The object of the Student class is being created here by new keyword and the object's value is being printed. A main () method is also being created inside the class, Look at the example attentively to understand the concept better:

class Student
{  
 //instance variable/properties  
 int stuid; 
 String name;  
 //create  main method in Student class  
 public static void main(String args[])
 {  
  //Creating an object or instance  
  Student stu1=new Student();//create an object  
  //Print values  
  System.out.println(stu1.stuid);
  System.out.println(stu1.name);  
 }  
}  

Object and Class Example: main outside the class

The programmers create classes and then use it from another class in real time development. This approach is said to be a better approach than the previous one.

Here is a simple example in which main () method is in another class. The programmer can have multiple classes in different Java files or single Java file. If the programmer defines multiple classes in a single Java source file then it is considered as a good idea to save the file name with the class name that has main() method. Have a close look at the example to grasp the concept better:

class Student{  
 int stuid;  
 String name;  
}  
//Defining another class DemoStudent that contains the main method  
class DemoStudent
{  
 public static void main(String args[])
 {  
  Student stu1=new Student();  
  System.out.println(stu1.stuid);  
  System.out.println(stu1.name);  
 }  
}  

Ways to initialize an object in Java

There are generally three ways to initialize object in Java that are depicted below:

  • By reference variable
  • By method
  • By constructor
Let's understand these three ways with the help of an example each

1. Object and Class Example: Initialization through reference

Initializing an object in Java basically means storing data into the object. Here is a simple example in which we are going to initialize the object through a reference variable.

Observe the example closely to understand the concept:

class Student
{  
 int stuid;  
 String name;  
}  
class DemoStudent{  
 public static void main(String args[])
 {  
  Student stu1=new Student();  
  stu1.stuid=001;  
  stu1.name="Anand";  
  System.out.println(stu1.stuid+" "+stu1.name);
 }  
}  

2. Object and Class Example: Initialization through method

In this very simple example, it is depicted that the two objects of Student class are being created and the value to these objects are being initialized by invoking the insertRecord method. Here, the state (data) of the objects is being displayed by invoking the displayInformation() method.

Please observe the example carefully to understand the concept from a deeper level:

class Student{  
 int stuid;  
 String name;  
 void savData(int a, String b)
 {  
  stuid=a;  
  name=b;  
 }  
 void showData(){System.out.println(stuid+" "+name);}  
}  
class DemoStudent{  
 public static void main(String args[])
 {  
  Student stu1=new Student();  
  Student stu2=new Student();  
  stu1.savData(001,"Abhi");  
  stu2.savData(002,"Nitin");  
  stu1.showData();  
  stu2.showData();  
 }  
}  

3. Object and Class Example: Initialization of the object through a constructor in Java

Here is an example of the object initialization through a constructor in Java depicted below:

public class  DemoStudent
{
   public DemoStudent() 
   {

   }

   public DemoStudent(String name) 
   {
      // This constructor has one parameter, name.
   }
}

Object and Class Example of Student

Here is a simple example in which the process of maintaining the records of the students is depicted, observe the example to understand the process of keeping records:

class Student{  
    int stuid;  
    String name;  
    float marks;  
    void insert(int i, String n, float m) 
	{  
        stuid=i;  
        name=n;  
        marks=m;  
    }  
    void show()
	{
	System.out.println(stuid+" "+name+" "+marks);
	}  
}  
public class TestStudent 
{  
public static void main(String[] args) 
{  
    Student stu1=new Student();  
    Student stu2=new Student();  
    Student stu3=new Student();  
    stu1.insert(001,"Nitin",95);  
    stu2.insert(002,"pankaj",90);  
    stu1.show();  
    stu2.show();  
}  
}  
Output :
1 Nitin 95.0
2 Pankaj 90.0

The various ways to create an object in the Java language

There are generally various ways that can be used to create an object in the Java language. Here are some of the ways depicted below:

  • By deserialization
  • By factory method
  • By new keyword
  • By newInstance() method
  • By clone() method

Anonymous object in Java

Anonymous in general terms simply means nameless hence; an object that has no reference is basically known as an anonymous object. The anonymous object can be used only at the time of creation of the object. In case the programmer have to use an object only once then the anonymous object is considered to be a good approach.

Here is an example depicting the same:

new Calculation();//anonymous object  
Here is an example of calling method through a reference:
Calculation c=new Calculation();  
c.fact(5);  

No Sidebar ads