|
Page 1 of 1
|
[ 1 post ] |
|
mkalaiyarasan
As a Family
Joined:
Tue Aug 25, 2009 5:25 pm
Beans: 35
In Bank: 0
College: studied
Degree: BE
Department: cse
State: Tamilnadu
Fav Quote: cse
Position: Employee
|
|
Fri Aug 28, 2009 4:20 pm
|
Post subject: Objects
What is an object? Create objects with the new keyword followed by a constructor. For example, the following program creates a TwoDPoint object and prints its fields: class OriginPrinter {
public static void main(String[] args) { TwoDPoint origin; // only declares, does not allocate // The constructor allocates and usually initializes the object origin = new TwoDPoint(); // set the fields origin.x = 0.0; origin.y = 0.0; // print the two-d point System.out.println( "The origin is at " + origin.x + ", " + origin.y); } // end main } // end OriginPrinter The . is the member access separator. A constructor invocation with new is required to allocate an object. There is no C++ like static allocation. To compile this class, put it in a file called OriginPrinter.java in the same directory as TwoDPoint.java and type: $ javac OriginPrinter.java What does this produce? Is this a complete program now? Can you run it?
|
|
|
|
|
Page 1 of 1
|
[ 1 post ] |
|