I can't understand getter and setter can someone explain | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

I can't understand getter and setter can someone explain

getter and setter

27th Sep 2017, 7:25 AM
Abinesh N
Abinesh N - avatar
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
27th Sep 2017, 7:47 AM
R4xx4r
R4xx4r - avatar
+ 3
with getter and setter, you can initialize and read a private value of your class
27th Sep 2017, 9:05 AM
MBZH31
MBZH31 - avatar
+ 2
thank you now i'll understand
27th Sep 2017, 9:10 AM
Abinesh N
Abinesh N - avatar