Java Object and Class

    Object and Class

    In this tutorial, we will get detailed knowledge about the objects, classes and object-oriented concepts.

    What is an object in Java

    An object is an entity that has its state and behaviour which can be logical or physical. For example- car, keyboard, pen etc.

    An object considered to have below three characteristics-

    • State- specifies the object’s value.
    • Behaviour- specifies the object’s functionality.
    • Identity- it can be implemented by the unique ID which might not be visible to the user. It is mostly used by the JVM to identify the object.

    An object is an instance of the class which holds memory. An object is considered to be the run-time real-world entity.

    What is a class in Java

    A class is considered to be the collection of the objects that holds the same property. A class is a blueprint that creates an object. Unlike the object, the class is a logical entity but not the physical entity. A class is a collection of-

    • Fields
    • Methods
    • Constructors
    • Blocks
    • Interfaces

    Class syntax-

    class <class_name>{  
    
        Field or variable or constant;  
    
        method;  
    
    }  

    What is an instance variable

    An instance variable is that is created within the class body but outside the method body. At the compile-time, there is no memory allocated to the instance variable but at the run-time when an object is created hence called the instance variable.

    What is a method in Java

    A method works like a function that exposes the object’s behaviour. It allows you to reuse the code and optimize the code.

    What is a new keyword in Java

    This keyword allocates the memory at the run-time in the heap memory area.

    Example- Using the main method within the class

    class Student{   
    
     int id;
    
     String stu_name;  
    
     public static void main(String args[]){  
    
      Student s1=new Student();//creating an object 
    
      System.out.println(s1.id);
    
      System.out.println(s1.stu_name);  
    
     }  
    
    }  

    Output-

    0
    Null

    As there is no value is assigned to the variables so 0 and null is the output.

    Example- Using the main method outside the class

    class Student{  
     int id;  
     String name;  
    }  
    
    
    class DemoStudent{  
    
     public static void main(String args[]){  
    
      Student s1=new Student();  
    
      System.out.println(s1.id);  
    
      System.out.println(s1.name);  
    
     }  
    
    }  

    Output-

    0
    Null

    How you can initialize the object

    You can initialize objects in below three ways-

    • Using the reference variable
    • Using method
    • Using constructor

    Object initialization using the reference variable

    Example-

    class Student{  
      int id;  
      String name;  
    }  
    
    class DemoStudent{  
    
     public static void main(String args[]){  
       Student stu=new Student();  
       stu.id=101;  
       stu.name="Jacob";  
       System.out.println(stu.id+" "+stu.name);
     }  
    }  

    Output-

    101 Jacob

    Objects initialization using method

    Example-

    class Student{  
    
     int rollno;  
    
     String name;  
    
     void insertDetail(int r, String n){  
    
      rollno=r;  
    
      name=n;  
    
     }  
    
     void displayInformation(){System.out.println(rollno+" "+name);}  
    
    }  
    
    class DemoStudent{  
    
     public static void main(String args[]){  
    
      Student s1=new Student();  
    
      Student s2=new Student();  
    
      s1.insertRecord(1,"Jacob");  
    
      s2.insertRecord(2,"Sam");  
    
      s1.displayInformation();  
    
      s2.displayInformation();  
    
     }  
    
    }  

    Output-

    1 Jacob
    2 Sam

    Different ways to create an object in Java

    • Using a new keyword.
    • By newInstance() method
    • By clone() method
    • By deserialization
    • By factory method etc.

    What is an anonymous object

    It is defined as an object that does not has any reference and can be used at the object creation time only. It is a good option to use when you have to use an object only once.

    New DemoStudent();

    You can call a method using the anonymous object-

    New DemoStudent.<method_name>();

    How to create multiple objects of the same type

    DemoStudent s1= new DemoStudent(), r2=new DemoStudent();