Issue
I have this variable in my ViewModel:
private String chosenDay;
and when I want to set the chosen day I use setter function:
public void setChosenDate(String date){
chosenDay = date;
}
and when I want to use it,I use getter:
public String getChosenDay(){
return chosenDay;
}
My question is: Do you think its a good practice to use getter and setters for the viewModel or I can just access the variable directly from the activity?
viewModel.chosenDay = "Monday"
Thank you !
Solution
It really depends on how you want to write your code. If you wish to follow OOP principles yes your example is the right way. You are doing "Encapsulation".
Encapsulation is one of the four fundamental OOP concepts.
Benefits of Encapsulation:
- The fields of a class can be made read-only or write-only.
- A class can have total control over what is stored in its fields.
Answered By - Mustafa Kuloğlu
Answer Checked By - Cary Denson (JavaFixing Admin)