Play AlgoQuiz

Rate Us :

Share with Friends :


Classes & Objects :

Class :

Class is a building block of an object oriented programming. Class is a collection of fields (fields are nothing but variables) and methods. Class is a blueprint of object. Class defines the structure of object. It is a planning before consisting object. It is a data type and meta data (i.e. data of data). Class is reusable code component or module.


building :

It refers to a basic unit from which something is built up.


instance :

It refers to an example or single occurrence of something.

In computing, an instance is nothing but allocating memory for variables exist within class.

Object :

Object is an instance of a class. In object oriented programming, data is represented as object. An instance is nothing but allocating memory for variables exist within class. Using one class, programmer can create any number of objects or instances.

Every object is having three properties :

  • State

  • Behaviour

  • Identity


attribute :

It refers to a quality or feature regarded as a characteristic or inherent part of someone or something.

State :

The value given to an attribute of an object define state. State of an object can be changed using behaviour. State is nothing but data hold by object.

Behaviour :

An operation performed on object is called behaviour.

Identity :

Each object is identified by the unique name.

The variables declared inside class is of two types :

  • Instance Variable

  • Class Variable

Note :

The variables declared within class define state of object.

Instance Variable :

A non - static variable of a class is called instance variable. Instance variable's memory is allocated on creation of object. Every object having it's own state, which is defined by using instance variable. This variable bind with object name.

Example :

class Book { String bname; String author; float price; }

These are instance variables because no static keyword is used. These are called when object is created.

How to create object?

In Java objects are dynamic. These are created during execution of program. All objects are created within managed memory area called heap area. Java allows to create object in three ways :

  • Using new operator.

  • Class.forName("classname").newInstance()

  • Factory method

new operator :

new is a operator which perform three operations :

  • Loading (hard disk to RAM - Random Access Memory)

  • Instantiating (allocating memory for non - static variables)

  • Initialization

Loading :

Copying ".class" files or class from secondary memory to primary memory.

Instantiation :

Allocating memory for non - static variables of a class (creating object).

Initialization :

Assigning default values to object is called initialization.

Syntax : new class_type_name();

Example :

new Book();

Default Value :

  • int, short, long, byte - 0 (default value)

  • float, double - 0.0 (default value)

  • char - \00000 (default value)

  • boolean - false (default value)

  • Reference - null (default value)

Reference variable :

A variable of type class is called reference variable. Reference variable is used to store an address/hashcode of object. A reference variable is required in order to reach an object by using program. Reference variable always refer a non - static class.


hashcode :

It refers to a way of getting a number from an object so it can be stored in a Hashtable. In Java a hash code can be any integer, and each object type is responsible for generating its own.

Syntax : class_type_name variable_name;

Example :

Book book1,book2; book1 = new Book(); book2 = new Book();

Reachable object or Referenced object :

An object which binds with reference variable is called reachable object.

Unreachable object :

An object which doesn't binds with reference variable is called unreachable object. All unreachable object automatically removed by JVM.

Creating class & object :

Example :

class A { int x; float y; double z; boolean b; char c; String s; } class B { public static void main(String [ ] args) { A obj1; obj1 = new A(); System.out.println(obj1.x); System.out.println(obj1.y); System.out.println(obj1.z); System.out.println(obj1.b); System.out.println(obj1.c); System.out.println(obj1.s); } }

Output :

Save the file as B.java javac B.java java B 0 0.0 0.0 false null


MiniDoll :

MiniDoll is an application to learn with fun. You can play it to solve some tricky questions related to Java Programming. Either enjoy playing it alone or discuss with your friends to solve it. It is a simple approach to learn by solving questions. This application has different topics of Java and each topic is divided into three phases. Start solving from initial topic to final topic and by the end of every topic you can boost your concept of that topic.