Monday, 20 January 2014

Using object in java


As you know, a class provides the blueprint for objects; you create an object from a class.A typical Java program creates many objects, which as you know, interact by invoking methods. Through these object interactions, a program can carry out various tasks, such as implementing a GUI, running an animation, or sending and receiving information over a network. Once an object has completed the work for which it was created, its resources are recycled for use by other objects.


creating a object

 In java to create an object has thre  parts.They are Declaration,Instantiation,initialization.In Declaration: The code set in bold are all variable declarations that associate a variable name with an object type.In Instantiation: The new keyword is a Java operator that creates the object.In Initialization: The new operator is followed by a call to a constructor, which initializes the new object

Let’s look at syntax of object declaration

classname objectname=new classfunction();

  (or)

classname objectname;
objectname=new classfunction();

Example for object declaration


class Areaexp
{
double len;
double brdh;
}
class Areaexp1
{
public static void main(String args[])
{
Areaexp rec=new Areaexp(); //declaring object
double area;
rec.len=6;
rec.brdh=10;
area=rec.len * rec.brdh;
system.out.println("Area is:" +area);
}
}

This would produce the following results

Area is :60


No comments:

Post a Comment