Friday, July 4, 2008

JAVA-7

Points:-
What is a class?
Fields say what an object is
Methods say what an object doesclass TwoDPoint {
double x;
double y;

}
To compile this class, put it in a file called TwoDPoint.java and type:
$ javac TwoDPoint.java

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

Multiple Objects:-
In general there will be more than one object in any given class. Reference variables are used to distinguish between different objects of the same class.
For example, the following program creates two two-d point objects and prints their fields:class TwoPointPrinter {
public static void main(String[] args) {
TwoDPoint origin; // only declares, does not allocate
TwoDPoint one; // only declares, does not allocate
// The constructor allocates and usually initializes the object
origin = new TwoDPoint();
one = new TwoDPoint();
// set the fields
origin.x = 0.0;
origin.y = 0.0;
one.x = 1.0;
one.y = 0.0;
// print the 2D points
System.out.println(
"The origin is at " + origin.x + ", " + origin.y);
System.out.println("One is at " + one.x + ", " + one.y);
} // end main
} // end TwoPointPrinter
one and origin are two different reference variables pointing to two different point objects. It's not enough to identify a variable as a member of a class like x or y in the example above. You have to specify which object in the class you're referring to.


Multiple Objects:-
It is possible for two different reference variables to point to the same object.
When an object is no longer pointed to by any reference variable (including references stored deep inside the runtime or class library) it will be marked for garbage collection.
For example, the following program declares two TwoDPoint reference variables, creates one two-d point object, and assigns that object to both variables. The two variables are equal.class EqualPointPrinter {
public static void main(String[] args) {

TwoDPoint origin1; // only declares, does not allocate
TwoDPoint origin2; // only declares, does not allocate

// The constructor allocates and usually initializes the object
origin1 = new TwoDPoint();
origin2 = origin1;

// set the fields
origin1.x = 0.0;
origin1.y = 0.0;

// print
System.out.println(
"origin1 is at " + origin1.x + ", " + origin1.y);
System.out.println(
"origin2 is at " + origin2.x + ", " + origin2.y);

} // end main

} // end EqualPointPrinter
origin1 and origin2 are two different reference variables referring to the same point object.

No comments: