Friday, July 4, 2008

JAVA-8

Static Fields:-
Static or class fields belong to a class, not to an object
class Point {
double x;
double y;
static double xorigin = 0.0;
static double yorigin = 0.0;
}
System.out.println("The origin is at (" + Point.xorigin + ", "
+ Point.yorigin + ")");
You access class variables with the name of the class rather than a reference variable.


Methods:-
Methods say what an object does. class TwoDPoint
{
double x;
double y;
void print() {
System.out.println(this.x + "," + this.y);
}
}
Notice that you use the Java keyword this to reference a field from inside the same class. TwoDPoint origin = new TwoDPoint();
origin.x = 0.0;
origin.y = 0.0;
origin.print();
noun-verb instead of verb-noun; that is subject-verb instead of verb-direct object.
subject-verb-direct object(s) is also possible.


Passing Arguments to Methodsclass:-
TwoDPoint {
double x;
double y;
void print() {
System.out.println("(" + this.x + "," + this.y + ")");
}
void print(int n) {
for (int i = 0; i < n; i++) {
System.out.println("(" + this.x + "," + this.y + ")");
}
}
To use this class, you might have some lines like these in a separate class and file: TwoDPoint origin = new TwoDPoint();
origin.x = 0.0;
origin.y = 0.0;
origin.print(10);
Note that there are two different print() methods. One takes an argument. One doesn't. As long as the argument lists can disambiguate the choice, this is allowed. This is called overloading.
Also note, that the System.out.println() we've been using all along is an overloaded method.
main(String[] args) is a non-overloaded method that has an array of strings as arguments.


Returning values from methods:-
class TwoDPoint {
double x;
double y;
void print() {
System.out.println("(" + this.x + "," + this.y + ")");
}
String getAsString() {
return "(" + this.x + "," + this.y + ")";
}
}TwoDPoint origin = new TwoDPoint();
origin.x = 0.0;
origin.y = 0.0;
String s = origin.getAsString();
System.out.println(s);
Better yet, TwoDPoint origin = new TwoDPoint();
origin.x = 0.0;
origin.y = 0.0;
System.out.println(origin.getAsString());



setter methods:-
Also known as mutator methods, setter methods just set the value of a field (often private) in a class. class TwoDPoint {
double x;
double y;
String getAsString() {
return "(" + this.x + "," + this.y + ")";
}
void setX(double value) {
this.x = value;
}
void setY(double value) {
this.y = value;
}
}TwoDPoint origin = new TwoDPoint();
origin.setX(0.0);
origin.setY(0.0);
System.out.println(origin.getAsString());


getter methods:-
Also known as accessor methods, getter methods just return the value of a field in a class. class TwoDPoint {
double x;
double y;
String getAsString() {
return "(" + this.x + "," + this.y + ")";
}
void setX(double value) {
this.x = value;
}
void setY(double value) {
this.y = value;
}
double getX() {
return this.x;
}
double getY() {
return this.y;
}
}
TwoDPoint origin = new TwoDPoint();
origin.setX(0.0);
origin.setY(0.0);
System.out.println("The x coordinate is " + origin.getX());

No comments: