Java - Class and Object

  • Object is an instance of a class.
  • Class is a template or blueprint from which objects are created. So object is the instance(result) of a class.
  • Object is the physical as well as logical entity whereas class is the logical entity only.
  • An entity that has state and behavior is known as an object e.g. chair, bike, marker, pen, table, car etc.

Class in Java :

A class is a group of objects that has common properties.
A class in java can contain:
  1. data member
  2. method
  3. constructor
  4. block

Syntax :

							  
class <class_name>{  
    data member;  
    method;  
}

							  

In this example, we have created a Student class that have two data members id and name. We are creating the object of the Student class by new keyword and printing the objects value.

class Student1{  
 int id=10;//data member (also instance variable)  
 String name="java";//data member(also instance variable)  
  
 public static void main(String args[]){  
  Student1 s1=new Student1();//creating an object of Student  
  System.out.println(s1.id);  
  System.out.println(s1.name);  
 }  
}

							  


Output:10 Java

							  

BytecodeTechnosolutions.com | Raju