Java Class and Object in detail


As java is an object oriented programming language, everything should be within the class. A class is nothing but a collection of variables (also called fields or data) and methods (functions).
Variables define state and methods define the behaviour.

A class is act as a template (for example - employee) and object as instance (for example - John, Tom etc.) of that template.

Or we can say that class is a collection of objects of similar types (for example ? employee class can have objects like John, Tom etc but not tiger, mango etc.

General Form of Class -

A class is declared using the keyword "class" followed by the user defined class name.

class Class_Name
{
 datatype variable 1;
 datatype variable 2;
  .
  .
 datatype variable n;

 access_specifier     return_type    method_Name(parameters...)
 {
  //body of method
 }
 .
 .
 access_specifier     return_type    method_Name(parameters...)
  {
    //body of method
  }
 } 

  • A class can have any number of variables and methods.
  • Variables are called the instance variable because a new copy of these variables is created each time a new instance (object) of class created.
  • No memory is allocated for instance variables.
  • Collectively variables and methods are called the members of the class.


Example -


class Employee
{
 int id;
 String name;
 int salary;
 float tax;
 
 public void showData()
 {
  System.out.println(id+"  "+name+"   "+salary+"   "+tax);
 }
}


As we already know class does not exist physically, only objects exist physically. So by defining Employee class we only create a template and that does nothing until we create objects of that class.
Object of a class can be created as follows -
Class_Name  object_name = new Class_Name();
So for employee class we can create object as follows -
Employee emp1 = new Employee();
Objects in java are created using the new keyword, It dynamically allocate memory for object and return a reference to that object. The reference is nothing but the address of the object in memory.
Employee emp1;   //   declaring object
emp1 = new Employee();  
//  allocate memory and return address of object that is received in emp1

As shown above when object is created a separate copy of instance variables is created for that object and also the memory is also allocated for these variables. We can create any number of objects of a given class and each class will have their own separate copy of instance variables.

To access the variables and methods associated with an object we use dot(.) operator.
For example we can assign the value "John" to emp1's name as follows -
emp1.name="John";
Open the notepad and write the following program -

class Employee
 {
  int id;
  String name;
  int salary;
  float tax;
  public void showData()
  {
   System.out.println(id+"  "+name+"  "+salary+"  "+tax);
  }
 }

public class Demo
{
 public static void main(String args[]) 
 {
  Employee emp1 = new Employee();
  emp1.id = 1;    // accessing variables of emp1 object
  emp1.name="John";
  emp1.salary=20000;
  emp1.tax = 200;
  emp1.showData();   // accessing method
  }
}
     

Output -  1  John  20000  200.0
Note - We can define any number of classes in a single notepad file, but only a single class can have main() method and only that class can be public. And we have to save the file with name of class that contain main() method. So here we have to save the file with 'Demo' name.