Tuesday, 15 March 2016

Working with Objects in Java

Its simple and easy to work with objects in Java. Here is a sample program explaining about the working with the objects in java.

Here the class box is the Object. The object - get - has w,h,b which is declared in double. The print will print the w,b and h.


class box
{
double w,h,b;
void get()
{
w= 12;
h=6;
b=4;
}
void print()
{
System.out.println("Width ="+w+"\n");
System.out.println("height="+h+"\n");
System.out.println("breadth="+b+"\n");
}
}
/*Then carry on with the main program. The below given is our main program*/
 
class boxdemo
{
 public static void main(String args[])
 {
box b = new box();
b.get();
b.print();
}
}
/*The program calls get and print. Thus the coding in the get and print get exectued*/
OUTPUT
H:\>cd java

H:\Java>path=C:\jdk1.3\bin;

H:\Java>javac boxdemo.java

H:\Java>java boxdemo
Width =12.0

height=6.0

breadth=4.0