wellnwill Marathahalli Bangalore

Class and Object in java


Java is an object oriented programming language. In java, we treat everything as an object.


Object

An object is a real world entity. Ex- Bulb, fan, Bike, Car, Human etc. An object is run time entity because memory will be allocated for an object while executing the program. An object has state, behavior and an identity.

State

State represents property (data) of an object i.e. data member (variable).

Behavior

Behavior represents functionality of an object i.e. member function (method).

Identity

The memory address of an object. For Example: Bulb is an object. Its name is Surya, watt is 200 etc. known as state. It is used to produces visible light, so lightening is the behavior.


Class

A class is a logical concept that describes the properties and functionalities of an object. Class is one kind of structure where we are defining the structure of an object and creating the object.That is why we say object is an instance (result) of a class.


Members of a class

Variables, Blocks, constructors, methods, nested class. Example of object and class: In below program, we have created Bulb class. We have defined data members i.e. watt and color. We are creating the object of Bulb class and printing the values of data members.

class Bulb
{  
   int watt; //data members or state
   String color;
   public static void main(String args[])
   {  
     Bulb b1=new Bulb();//creating an object of Bulb class 
     System.out.println(b1.watt);  
     System.out.println(b1.color);  
   }  
}  

// Output - 0, null

Example 2

In this program, we are creating Bulb class with some data members and creating the objects then assigning the value to data members. After assigning the values, we are printing the values of object’s data members.


class Bulb
{  
   int watt;  
   String color;
   float price; 
  
   void insertValue(int w, String c, float p)//Assigning the values to data members       
   {   
     watt=w;  
     color=c;  
     price=p;
     System.out.println("data inserted");
   }  

   void showValue()//Printing the values of data members
   {
     System.out.println("display data");  
     System.out.println("watt :"+ watt);
     System.out.println("color :"+ color);
     System.out.println("price :"+ price);
   } 
  
  public static void main(String args[])
  {  
     Bulb b1=new Bulb(); //creating object b1 and b2 of Bulb class          
     Bulb b2=new Bulb(); 
     b1.insertValue(100,"white",15.50F);  
     b2.insertValue(200,"blue",25.75F);  
     b1.showValue();  
    b2.showValue();
 }  
}



class loading

class information will be loaded from hard disk to RAM means all the static member will be loaded into class area.


class A
{
}
class Bulb
{
}
class Test
{
  public static void main(String args[])
  {
    A a1=new A();
    Bulb b1=new Bulb();
  }
}

In above program, we need to understand that when will classes be loaded and what will be the sequence. when we execute Test file i.e. Test.class at exection time.

means java Test---here jvm will use Test.class information first time then class loading of Test class will be started.

once execution of this statement (A a1=new A();) will be started at that time, we are using A class information means class loading of A class will be started.

once execution of this statement (Bulb b1=new Bulb(); will be started at that time, we are using Bulb class information means class loading of Bulb class will be started.

Background process when an object creation statement will be executed.

  • Bulb b1=new Bulb();
  • Eight bytes of memory will be assigned for the reference variable and initialize with null as default value.
  • JVM will verify whether class is loaded or not. If class is not loaded then class loading will be performed.
  • Class loading task
  • Read the byte code (.class file) from hard disk and loads into RAM memory.
  • Allocates memory for static variables into class area (class area is part of heap memory) and initializes with default and user specified values.
  • Execute static blocks if any.
  • Allocates the memory and initialize the default values for the object level variables (instance variables) inside the memory of an object.
  • JVM will assign the address of particular object to the reference variable.
Footer with Map