# Object and class in Java

&#x20;**Class:** A class can be defined as a template/blueprint that describes the behavior/state that the object of its type supports. A class is a group of objects that has common properties. “A class is a way of binding the data and associated methods in a single unit”

&#x20;A class in java contains:

* &#x20;Data Members&#x20;
* &#x20;Method&#x20;
* &#x20;Constructor
* &#x20;Block&#x20;
* Class and Interface

#### &#x20;Object:

* &#x20;Object is the physical as well as logical entity whereas class is the only logical entity.
* Object is an instance of class, object has state and behaviors.

&#x20;

> **Note:** Instance is a mechanism of allocating sufficient amount of memory space for data members of a class.

&#x20;

**Example:**&#x20;

A blueprint for a house design is like a class description. All the houses built from that blueprint are objects of that class. A given house is an instance.

&#x20;An Object in java has three characteristics:

* &#x20;State
* &#x20;Behavior
* &#x20;Identity

&#x20;**State:** Represents data (value) of an object.&#x20;

**Behavior:** Represents the behavior (functionality) of an object such as deposit, withdraw etc.

&#x20;Identity: Object identity is typically implemented via a unique ID. The value of the ID is not visible to the external user. But, it is used internally by the JVM to identify each object uniquely.&#x20;

You can get the unique id of an ID using following method.

&#x20;System.identityHashCode(obj);&#x20;

Class is also can be used to achieve user defined data types

#### &#x20;Example 1

&#x20;Animal Class:

&#x20;Dog, cat, and cow are belong to animal's class.

&#x20;Each object has state and behaviors. State: - color, name, height, age etc.

&#x20;Behaviors: - barking, eating, and sleeping etc.

#### &#x20;Example 2

&#x20;A blueprint for a house design is like a class description. All the houses built from that blueprint are objects of that class.

#### &#x20;Syntax to declare a Class

&#x20;class Class\_Name { data member; method; }**.**

#### &#x20;Steps for compiling and executing the java program&#x20;

Java is very simple programming language first we write a java program and save it with program class name.

&#x20;In below program we create a java program with "Test" name so we save this program with "Test.java" file name. We can save our java program anywhere in our system or computer.&#x20;

#### Example 1:

&#x20;class Test { public static void main(String\[] args) { System.out.println("Hello Testing Bar"); System.out.println("This is My First Java Program"); }

#### &#x20;Save Java Program

&#x20;Syntax: Filename.java

&#x20;Example: Test.java

#### &#x20;Compile and Execute Java Code&#x20;

To compile: javac Test.java

&#x20;To execute: java Test

#### &#x20;Output&#x20;

This is My First Java Program

&#x20;**Note:** Here Javac and Java are called tools or application programs or exe files developed by sun micro system and supply as a part of jdk 1.6/1.7/1.8 in bin folder. Javac is used for compile the java program and java is used for run the java program.

&#x20;**Note:** A java program can contain any number of main method but JVM start execution from that main() method which is taking array of object of String class.&#x20;

#### Example of Object and Class&#x20;

In this example, we have created an Employee class that has two data members, eid and ename. We are creating the object of the Employee class by new keyword and printing the objects value.

#### &#x20;Example 2

&#x20;ackage pack1;

&#x20;www.testingbar.com Sujoy@testingbar.com public class Employee

&#x20;                            { int eid=852; // data member (or instance variable) String ename="Rohit"; // data member (or instance variable)                                     &#x20;

&#x20;                                 public static void main(String args\[]) { Employee e=new Employee(); //                                      Creating an object of class Employee System.out.println("Employee ID: "+e.eid); System.out.println("Name: "+e.ename); } }  &#x20;

#### &#x20;Output&#x20;

Employee ID: 852&#x20;

Name: Rohit

#### &#x20;NOTE:

* &#x20; JAVA always follows dynamic memory allocation but not static memory allocation.&#x20;
* In order to create a memory space in JAVA we must use an operator called new. This new operator is known as dynamic memory allocation operator.

#### &#x20;Syntax 1 for defining an OBJECT:&#x20;

**objname** = new Clsname represents name of the class. Objname represents JAVA valid variable name treated as object. New is called dynamic memory allocation operator.

&#x20;**Clsname** () represents constructor. The new operator will perform two standard actions. They are:

&#x20;1\. It allocates sufficient amount of memory space for the data members of the class.

&#x20;2\. It takes an address of the class and stored in the left hand side variable of syntax 1

#### &#x20;Syntax 2 for defining an OBJECT:&#x20;

objname; //object declaration

&#x20;Objname = new; //object refrencing&#x20;

#### Example 3

&#x20;package pack1;

public class Employee

&#x20;{ www.testingbar.com Sujoy@testingbar.com int eid=1020; String eName="Sujoy"; void show() { System.out.println("eid="+eid); System.out.println("eName="+eName); } public static void main(String\[] args)

&#x20;{ Employee obj = new Employee(); Employee obj2 = new Employee(); obj.show(); obj2.show();

#### &#x20;Output:

&#x20;eid=1020&#x20;

eName=Sujoy

&#x20;eid=1020&#x20;

eName=Sujoy Example: 4

&#x20;package pack1;&#x20;

public class Employee { int eid; String eName; void setValue(int a,String b) { eid=a; eName=b; } void show() { System.out.println("eid="+eid);&#x20;

System.out.println("eName="+eName); } public static void main(String\[] args) { Employee obj1 = new Employee(); Employee obj2 = new Employee(); obj1.setValue(747, "Sujoy"); obj2.setValue(856, "Rohit");

#### &#x20;Output:

&#x20;eid=747&#x20;

eName=Sujoy

&#x20;eid=856

&#x20;eName=Rohit

![](https://106491783-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LTW0eWU2biq082hmKEn%2F-LTa7m8LG_KJDj-63TRt%2F-LTaEeM7RhydxnV6hEvt%2Fimage.png?alt=media\&token=35ac0cc3-bfdb-4dd6-a492-2d89883ca83c)

