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?

Tassisto
  • 9,877
  • 28
  • 100
  • 157
  • 5
    Yes, they are simple methods. – BackSlash Apr 13 '16 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. – Ramesh Kumar Apr 13 '16 at 15:09
  • Yes, simple methods which are usually called accessors and mutators, for getters and setter respectively. – dambros Apr 13 '16 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; }`` – f1sh Apr 13 '16 at 15:09
  • 1
    @f1sh [Lombok](https://projectlombok.org/features/GetterSetter.html) is an interesting way using annotations and metaprocessing to emulate C#-esque `get; set;`. – callyalater Apr 13 '16 at 15:11

6 Answers6

2

Yes it's okay they are just methods!

Guillaume Barré
  • 4,168
  • 2
  • 27
  • 50
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.

Sergei Tachenov
  • 24,345
  • 8
  • 57
  • 73
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.

Ashish Patil
  • 4,428
  • 1
  • 15
  • 36
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."
randominstanceOfLivingThing
  • 16,873
  • 13
  • 49
  • 72
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.

Community
  • 1
  • 1
callyalater
  • 3,102
  • 8
  • 20
  • 27
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.