+ 1
I can't understand getter and setter can someone explain
getter and setter
3 Answers
+ 4
maybe its more clear with an example :)
class Clock {
String time;
void setTime (String t) {
time = t;
}
String getTime() {
return time;
}
}
class ClockTestDrive {
public static void main (String [] args) {
Clock c = new Clock;
c.setTime("12345")
String tod = c.getTime();
System.out.println(time: " + tod);
}
}
When you run the program, program starts in mains,
- object c is created
- function setTime() is called by the object c
- the variable time is set to the value passed by
- function getTime() is called by object c
- the time is returned
- It will passe to tod and tod get printed out
+ 3
with getter and setter, you can initialize and read a private value of your class
+ 2
thank you now i'll understand