+ 1
Is getter and setter are same as Scan & print tespectively?
2 Antworten
+ 3
It allows you to encapsulate your code. Once encapsulated, you can use the "getter" to get values back from it, and "setter" to set values to it. This way you can keep the variables and such private, but still be able to access them elsewhere easily. More secure.
http://www.geeksforgeeks.org/encapsulation-in-java/
"Advantages of Encapsulation:
Data Hiding: The user will have no idea about the inner implementation of the class. It will not be visible to the user that how the class is storing values in the variables. He only knows that we are passing the values to a setter method and variables are getting initialized with that value.
Increased Flexibility: We can make the variables of the class as read-only or write-only depending on our requirement. If we wish to make the variables as read-only then we have to omit the setter methods like setName(), setAge() etc. from the above program or if we wish to make the variables as write-only then we have to omit the get methods like getName(), getAge() etc. from the above program
Reusability: Encapsulation also improves the re-usability and easy to change with new requirements.
Testing code is easy: Encapsulated code is easy to test for unit testing."
+ 5
Getters and setters are used to indirectly retrieve or modify a class' or instance's member, some refer to them as Properties. Not really sure what you mean by Scan & print, but if you mean Scanner and print, I suppose they are used for I/O interaction, hope I get you right.
Hth, cmiiw