Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between a getter method and a method which returns the state of an instance variable?

Tags:

java

guava

I am reading through the Guava library, and I keep running into methods that look something like this:

@Override public int size() {
  return size;
}

What is the difference (strategically, conventionally, etc) between the above and the following?

@Override public int getSize() {
  return size;
}

Or is there no difference? Is it just shorthand?

like image 257
pgblu Avatar asked Sep 01 '25 18:09

pgblu


1 Answers

One form isn't using JavaBeans conventions. Functionally speaking, systems that expect you to follow those conventions will not work or be very cumbersome to set up if you use non-conventional getters/setters for your beans, but if you're not, then there's no real difference.

Since Guava has a lot of collections, and the Collection interface actually defines a size() method, my gut tells me that Guava is more inclined to follow the Collection interface than JavaBeans conventions.

like image 117
Makoto Avatar answered Sep 04 '25 06:09

Makoto