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.
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.
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.
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.
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.
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.
- Photo Credit pancarte image by Pauline Decroix from Fotolia.com
How to Write a Method in Java
Java is the most popular programming language, primarily because of its portability. The same Java program can be used on virtually any...
How to Set a Java Path
Once the Java SDK is installed the path variables must be set so that programs written in Java will be executable from...
How to Generate a Random Number in Java
The Java programming language is a powerful tool in computer science, providing limitless possibilities for all types of programs. Among Java's many...
How to Get an Image in Java
Java software is a tool available to web developers that allows users to create customize, detailed web pages for viewing on the...
How to Set Java String to Null
In Java programming, "null" is a special keyword reserved to refer to a variable that, while assigned a variable name, does not...
What Is Static in Java?
Java applications use Object Oriented development. In an Object Oriented project, programmers design a set of objects to carry out specific sections...