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.
A object of television class can be created in this way:
For example we take a Television class and create its object.
public class Television {
|
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"); |