Get and Set Method in Java
Java is an object-oriented programming (OOP) language. Object-oriented code is organized using a construct called a "class," which serves as a blueprint for creating an "object." An object is a collection of properties (member variables) and behaviors (methods). Typically, member variables are private and can accessed only by the object containing them. "Get" and "set" methods, also called "getters" and "setters," are special methods that interact with private member variables.
-
Function
-
Getters and setters, or "accessor" methods, provide access to an object's properties. A get method returns the value of a property held by an object. A get method has a return type matching the type of the associated member variable. Get methods typically do not take any parameters. A set method has a "void" return type and takes a parameter of the appropriate type to assign to the associated member variable.
Conventions
-
Accessor methods are used by external objects, and thus are declared as "public" (externally visible) methods. The accepted convention is to name get and set methods for the associated member variable (for example, "getName" and "setName," associated with the variable "name"), prefixed with "get" or "set." Not all member variables may have associated accessor methods. These methods are written only for values that need to be accessed externally.
-
Purpose
-
Restricting access to an object's member variables is part of a practice called "encapsulation." Encapsulation compartmentalizes the components of an object, hiding the implementation (internal workings) of the object that do not need to be externally visible, and protecting internal data from unchecked modification. Accessor methods may perform calculations, validation, or other actions that would be bypassed by making member variables directly accessible.
Benefits
-
Protecting member variables using accessor methods allows programmers to easily change the internal code without changing the way the class is used by others. This avoids requiring code changes throughout an application when a single class is modified. Getters may perform calculations and actions required to produce a value, minimizing the amount of external code required to use the value. Setters may perform validation, preventing errors caused by member variables being set to invalid values.
Considerations
-
Adding accessor methods to a class should be done conservatively. Only member variables that require external access should have accessor methods. Properties that should only be modified by the internal workings do not need setters. Properties that have no external relevance should not have getters. This practice minimizes exposure of an object's implementation, and protects properties from modification that could introduce run-time errors. The fewer public methods there are, the more maintainable the code will be.
-
References
Resources
- Photo Credit pancarte image by Pauline Decroix from Fotolia.com