well, it was not the actual code, so copy & paste won't work (:
ok, here is the full!!! (:
point.java :
//this is simple point class, stores two integer, which are read-only.
//so you need to specify these two integer while object creation....
public class point {
private int x;
private int y;
public point(int x, int y) {
this.x = x;
this.y = y;
}
public int getx() {
return x;
}
public int gety() {
return y;
}
}
pointUser.java :
//this class simply creates a point, display something...
public class pointUser {
public static void main(String[] args) {
//some variables...
int x,y;
point mypoint_one = new point(15,20); //decleration and definition
point mypoint_two; //only decleration
System.out.println("mypoint_one : "+mypoint_one.getx()+ ", "+ mypoint_one.gety() );
x=100;
y=1;
mypoint_two = new point(x,y);
System.out.println("mypoint_two : "+mypoint_two.getx()+ ", "+ mypoint_two.gety() );
}
}
simply create these two files, and compile pointUser, then run pointUser...
it is very simple, in this example you create points, which have read-only properties x and y. you can't hava direct access to these members, but you can get their values by calling
mypoint_one.getx(); //returns mypoint_one.x
mypoint_one.gety(); //returns mypoint_one.y
you can (if you want) give write-access by writing "setter" methods
public void setx(int x) {
this.x = x;
}
public void sety(int y) {
this.y = y;
}
try putting these two setter methods to point.java and then add these lines inside your "main" method in pointUser.java
mypoint_one.setx(50);
mypoint_one.sety(10);
System.out.println("mypoint_one : "+mypoint_one.getx()+ ", "+ mypoint_one.gety() );
then recompile and run pointUser.
so why are we using this? instead just making everything "public"?
because we want controlled access to members of objects, with these "getters" and "setter" we can have control over members, if we want read-only we make only getter. if we want read-write, we make getter and setter, and so on (:
another thing is construction of point, point(int, int) constructor is simple, it just assign starting values... because we don't have setters in the point class, only place we can assign values is constructors.
well, try to adopt this point class to your CDRecord class, I think this will be a good excercise...
then when you feel comfortable with simple classes, you can move to array stuff...
well, hope this helps....