0

In Java, I have a bunch of fields and associated getter/setter methods. Do you prefer to group them per field or fields first then getter/setters?

Style 1:

int A  
int getA()

int B  
int getB()

Style 2:

int A
int B

int getA()
int getB()
1
  • I don't think this is just matter of getters and setters. Isn't this question also valid for other class methods?
    – starcorn
    Mar 11, 2011 at 9:50

7 Answers 7

5

This may not really answer your question, as I will not talk about style, but for trivial getters and setter I do not write any code anymore. Have a look at Projekt Lombok, were all you’d have to write would be:

@Getter @Setter int a;
@Getter @Setter int b;

And if this still looks like too much code, the annotations can also be at class level.

0
4

Maybe you should have a look on Java Code Conventions:

Put declarations only at the beginning of blocks. (A block is any code surrounded by curly braces "{" and "}".) Don't wait to declare variables until their first use; it can confuse the unwary programmer and hamper code portability within the scope.

This applies to methods, if/else blocks, loops, and, of course, classes.

So, the convention encourages for the second approach.

4
  • Misapplied here, and questionable approach anyway. Mar 14, 2011 at 17:16
  • @Andy Thomas-Cramer: Why, if I can ask? Mar 14, 2011 at 17:29
  • Misapplied - these declarations are not in blocks at all. Questionable approach - it's a anachronism that was once necessary in one of Java's ancestors, and to which older programmers are accustomed, but now is an unnecessary source of error and a hindrance to refactoring. Sutter and Alexandrescu offer more detail on the same issue in item 18: "Declare variables as locally as possible" of "C++ Coding Standards." Most of the same arguments apply. Mar 15, 2011 at 15:32
  • @Andy Thomas-Cramer: Hmmm, I supose I got confused by the A block is any code surrounded by curly braces "{" and "}". statement. And, moreover, I had got quite old, and thought that the Java objects still used object level variables, with public accesor methods. See, I am so out, that thought that the best place to review the Java conventions was the Java Code Conventions page, and not the C++ Coding Standards. Thanks for enlighten me. I will apply to learn the last tendencies at Java programing and standards (maybe at the MSDN page, or so). Mar 15, 2011 at 16:07
2

Style 3)

int a
int b

int getA()
int getB()
0

Personnally, style 2. Mostly depends on the coding standard of your team.

I prefer to have all the member variables on top, starting from public to private. Then all the methods again ordered by visibility.

0

Style 2 is what I've seen usually. But there are no hard rules I guess.

0

I would prefer to group them fields first followed by methods, but it really doesn't matter. All it matters is that whatever convention you choose is followed uniformly throughout the project.

0

I think you better declare the Variables before and the the Get/Set methods in a separated methode section, specially as some programmign languages doesnt allow you to declare procedures between the variables so if you move to program in one of these you will not have to get used to it. Besides separating functions from Variables makes it easier to search for a function when you need it.

Not the answer you're looking for? Browse other questions tagged or ask your own question.