A constructor in essance is a method (with the same name as the Class) that is executed when an instance of the class is created.
For Example:
public class foo {
public int x;
// Constructor
public foo() {
// Do something like
this.x = 45;
}
}
So when you enter in your application:
foo x = new foo() // the constructor foo() is executed.
jerbo
Junior Poster in Training
84 posts since Sep 2004
Reputation Points: 11
Solved Threads: 1
jerbo
Junior Poster in Training
84 posts since Sep 2004
Reputation Points: 11
Solved Threads: 1
Opps......this.diff()....should be .......this.diff
jerbo
Junior Poster in Training
84 posts since Sep 2004
Reputation Points: 11
Solved Threads: 1
Looking at your code:
public Clock(int hours, boolean isTicking, int diff){
this.hours = hours;
this.isTicking = isTicking;
this.diff = diff;
You should have hours, isTicking, and diff defined as class variables too. I did not see that in your code snippet.
To prevent confusion between class variables, and local (method) variables, I add an underscore to my private variables, so that I don't get them confused......ie:
public class CLock {
// Private variables to the class
private int hours_;
private isTicking_;
private diff_;
// Your constructor
public Clock(int hours, boolean isTicking, int diff){
this.hours_ = hours;
this.isTicking_ = isTicking;
this.diff_ = diff;
} // End of Clock Constructor
} //End of Clock Class
Hoope this clarifies things. If not, submit what errors you are receiving and the code.
jerbo
Junior Poster in Training
84 posts since Sep 2004
Reputation Points: 11
Solved Threads: 1