Tuesday, 16 July 2013

Getter and Setter Methods in Java...

'getter' is a term for a method that gets the value of a variable, a 'setter' sets the value. That's really all there is to it...

The Example of Getter and Setter method is given below:-

class Clock {
            String time;
 
            void setTime (String t) {
               time = t;
            }
 
            String getTime() {
            return time;
            }
   }
}
 
class ClockTestDrive {
   public static void main (String [] args) {
     
     Cock c = new Clock;
 
     c.setTime("12345")
     String tod = c.getTime();
     System.out.println(time: " + tod);
 
   }
}

No comments:

Post a Comment