1

Is the follwing true: Java getters and setters are just methods.

I know they are implemented to manipulate or get the value of private ... attributes/fields. But is it okay to call them methods?

5
  • 5
    Yes, they are simple methods. Commented Apr 13, 2016 at 15:07
  • Yes, they are simple method to retrieve the attributes/fields but they may have some checks or operations before setting or getting values. Commented Apr 13, 2016 at 15:09
  • Yes, simple methods which are usually called accessors and mutators, for getters and setter respectively. Commented Apr 13, 2016 at 15:09
  • 1
    Yes, they are. Unfortunately there's no elegant way in java to have getters and setters other than to write or generate them, which results in boring boilerplate code. In C# you can define getters and setters at the declaration of the fields such as: string MyProperty { get; set; } Commented Apr 13, 2016 at 15:09
  • 1
    @f1sh Lombok is an interesting way using annotations and metaprocessing to emulate C#-esque get; set;. Commented Apr 13, 2016 at 15:11

6 Answers 6

2

Yes it's okay they are just methods!

Sign up to request clarification or add additional context in comments.

2 Comments

And for instance they are indistinghuishly from "virtual" calculated fields like getAge() (based on date of birth).
call the method calculateAge()
1

Technically, from the language and VM point of view, yes, they are just methods.

Some libraries and frameworks, however recognize their special nature. For example, JPA may be used to map “properties” by annotating either fields or methods. Beans Binding library uses them to access properties, so if you have a getText() / setText() pair on some object, then you can bind the “text” property to some other property of some other object (you'll still have to do the addPropertyChangeListener magic, though). But this is just a “convention over configuration” phenomena combined with the power of Reflection.

Comments

0

As per JLS,

A method declares executable code that can be invoked, passing a fixed number of values as arguments.

And this criterias satisfies to getters and setters as well; so we can say the are "methods" in java language.

Comments

0

Luckily I was able to pull Craig Larman's "Applying UML and Patterns" book section in google. As quoted

Accessing methods retrieve(accessor method) or set(mutator method) attributes. In some languages such as Java it is a common idiom to have an accessor and mutator for each attribute, and to declare all attributes private(to enforce data encapsulation). The methods are excluded in the class diagram because of the high noise-to-value ratio they generate." 

Comments

0

Java Getters and Setters are accessor methods. So, yes, they are methods.

Many programmers (Java or otherwise) may feel annoyed at constantly writing getXXX() and setXXX(type t) for all of their private fields, especially if they are basically just one line methods. For this case, there are some annotation libraries (like Lombak) that generate these through the power of metaprogramming and Java annotations and reflection.

However, there are many reasons to explicitly write getters/setters. A good list can be found here. But I really like this answer, too.

Comments

0

Yes, essentially they are methods. The standard definition for a Java method is as follows;

A method is a set of code which is referred to by name and can be called (invoked) at any point in a program simply by utilizing the method's name.

-https://mathbits.com/MathBits/Java/Methods/Lesson1.htm

So you can consider methods as small programs within a class itself that allows us to fulfill specific tasks, which is also exactly what getters and setters do.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.