Get and Set Method in Java

Get and Set Method in Java thumbnail
"Get" and "set" methods provide access to restricted properties in Java objects.

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.

  1. 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.

Related Searches:

References

Resources

  • Photo Credit pancarte image by Pauline Decroix from Fotolia.com

Comments

You May Also Like

  • ArrayList Methods in Java

    The Java computer programming language provides a built-in list interface, which defines an ordered sequence of items and the operations that can...

  • Java Tutorial on the Random Method

    Randomly generated numbers have many uses in computer programs, such as creating unpredictability in games, modeling simulations and performing encryption. Java ...

  • 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...

  • What Is a Constructor Method in Java?

    Java is an object-oriented programming language used to create applications in one environment that can run in another environment without needing to...

  • How to Calculate a Median in Java

    There is no "calculate median" method already built into Java, but that is no obstacle when the time comes to find the...

  • Java Random Number Method

    Random Number methods in Java return random numbers for use in the program. There are two ways to generate a random number...

  • How to Set Up a Whiteboard With Java Applets

    The Internet has opened a new medium for collaboration. Students and colleagues no longer have to be in the same physical location...

  • How to Get Keyboard Input in Java

    Capturing keyboard input in a Java console program is fairly straightforward and only requires a few lines of code. Console programs are...

Related Ads

Featured