Sunday, January 30, 2011

JAVA Programming : Object

JAVA Objects

As you know from the previous post, class is a blueprint for object. So, an Object is an instance of class that Java Virtual Machine (JVM) create at runtime whenever it finds an new keyword. Every object will have state and behavior defined in its class. 
To understand software objects, first we look at some example of real world object like a Fan. A fan will have its own state like motor, blade, power switch etc. and behavior like speed, on or off etc. In the same way there are software objects that too have state in form of fields or variables and behavior in form of methods or functions.

Creating an Object:
For example we take a Television class and create its object.

public class Television {

    // the Television class has two fields
    public int size;
    public String  colorType;

    // the Television class has one constructor
    public Television(int tvSize, String tvColorType) {                      

        size = tvSize;
        colorType = tvColorType;

 A object of television class can be created in this way:

                 Television tv = new Television(12,"Color TV");              

In a object Declaration Statement there is three part
1. Declaration of object : Part of statement before the '=' sign is Declaration part of object.
2. Instantiation : The new keyword in Java does the instantiation of an object.
3. Initialization : After new keyword there is call to construction of class which does the initialization.

It is not necessary that declaration part is to followed by instantiation and initialization part. Like other variable declarations, object declarations can be done alone. A object declaration can be done in this way :

                 Television tv;   
                 tv = new Television(12,"Color TV");                         

1 comment:

  1. It's hard to find educated people about this topic, but you sound like you know what you're talking about!

    Also visit my page; no credit checks loans
    Also see my web site > see this great post