Alfred’s Computing Weblog

Alfred Java-cored Computing Weblog

Class vs Object vs Instance

with 9 comments

In OO Programming, we often hear of terms like “Class”, “Object” and “Instance”; but what actually is a Class / Object / Instance?

In short, An object is a software bundle of related state and behavior. A class is a blueprint or prototype from which objects are created. An instance is a single and unique unit of a class.

Example, we have a blueprint (class) represents student (object) with fields like name, age, course (class member). And we have 2 students here, Foo and Bob. So, Foo and Bob is 2 different instances of the class (Student class) that represent object (Student people).

Let me go into details…

Object
Real world objects shares 2 main characteristics, state and behavior. Human have state (name, age) and behavior (running, sleeping). Car have state (current speed, current gear) and state (applying brake, changing gear). Software objects are conceptually similar to real-world objects: they too consist of state and related behavior. An object stores its state in fields and exposes its behavior through methods.

Class
Class is a “template” / “blueprint” that is used to create objects. Basically, a class will consists of field, static field, method, static method and constructor. Field is used to hold the state of the class (eg: name of Student object). Method is used to represent the behavior of the class (eg: how a Student object going to stand-up). Constructor is used to create a new Instance of the Class.

Instance
An instance is a unique copy of a Class that representing an Object. When a new instance of a class is created, the JVM will allocate a room of memory for that class instance.

About these ads

Written by Alfred

July 8, 2008 at 13:49

Posted in Java

Tagged with

9 Responses

Subscribe to comments with RSS.

  1. Thank you very much alfred. your’s explanation of the oops is very nice. i had some of the confusions in the oops concepts but now that are cleared. once again i thank you.

    gkaruppusamy

    November 26, 2009 at 14:59

    • Hi alfred. I have some doubts on this statement..

      class Book {

      public static void main (String arg[]) {

      Book b1 = new Book ();
      }
      }

      Now explain clearly,

      Why? : public static void main (String arg[])

      Why? : Book b1 = new Book ();

      What is the use of new?
      Which one is object, and which one is instance?
      Does Book() constructor returns reference to obj b1?

      Thanx and Regards
      Ganesh Slv

      Ganesh Slv

      April 14, 2010 at 20:02

      • Ganesh,
        Which part of the Alfred’s explanation are referring to with your questions? I cannot connect what you asked to the content that is presented. I would think that alfred is NOT OBLIGATED to explain what is not covered in this content. It seems like a branch-out question.

        Anyway this is what I know about question 1:
        the main object is link the class Book to command line argument when you run the java program in MS-DOS. It is not needed if you don’t want to run this program in MS-DOS. It might true for unix too but I am not sure.

        About question 2: This is odd. The Book constructor is creating a new instance of Book. It is created itself. It is a recursive relationship. I am not sure if it would work as intended.

        Recursive methods will fine but I have not tried recursive classes. Hope you find a better answer to your question 2.

        J1

        August 22, 2010 at 6:56

      • A class is a program construct which encapsulates data and operations on data. It is a blue print of an object.

        An object can be called as an ‘instance’ of a class.

        The variables and methods (without static key word) that are declared in the class, but not in the methods are called as instance variables which have one copy per object. Once an object is created, it is said that, an instance of the class is created.

        Class variables and methods occur once per class where as the instance variables and methods occur once per instance of a class (per object).

        In your case,
        b1 is an instance of object book.
        new is used to create a new instance or a copy of the book object!

        Similarly you can create new instance b2 using “new” keyword which will be object of “book”.

        well a constructor can be said to return a reference of the book object to the instance b1….am not sure of this as i am new too!

        Hope you got something!

        Vish

        February 16, 2011 at 2:27

  2. Thanx a lot ..i have clere littele bit about object and instance
    if possible plz give ne a more example releted to application..and i want to know how this obj and instance michanism work in memory

    Amrin

    August 14, 2010 at 3:22

  3. A clear answer for Object vs instance

    Anonymus

    July 3, 2011 at 4:59

  4. Thanks so much

    anhc

    July 6, 2011 at 17:13

  5. This makes sense. Thanks for explanation.

    Jay

    April 1, 2012 at 9:54

  6. Everything is very open with a clear clarification of the issues.
    It was definitely informative. Your site is extremely helpful.
    Thanks for sharing!

    dimension fenetre pvc

    June 5, 2013 at 1:00


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: