0

I've been searching around to see if there is a convention for this, but I don't see one. I would like to see what the community at large does.

When you have any class in Java, do you organize the methods in any particular way? Is there a convention for this.

For example, perhaps listing public static members first, then public members, then private members? Perhaps alphabetical order? Perhaps in order of how the methods will get called, that is, initialization methods first, worker/helper methods next, clean up methods last?

What do you do, and why? Is there a suggested way to do this?

Andi Jay
  • 5,882
  • 13
  • 51
  • 61
  • 2
    The question is opinion based, as there is no *official* way to do that. You may read the following http://geosoft.no/development/javastyle.html – Alessandro Da Rugna May 24 '16 at 12:22
  • 1
    It's up to you and your team's discretion. Just make sure that your maintaining `cohesion` in the code of your program. – A.Sharma May 24 '16 at 12:22
  • 2
    Since your IDE will help you find and navigate all members, it is not necessary to order them. –  May 24 '16 at 12:23
  • Others have suggested you correctly. If you are still not comfy with the order of your methods, why not just sorting them alphabetically? – David Tabernero M. May 16 '18 at 20:20
  • See these answers, +100 votes https://stackoverflow.com/q/4668218/4930344 – hyperpallium Jun 24 '19 at 09:52

3 Answers3

11

The relevant section in Google's Java Style Guide says:

The ordering of the members of a class can have a great effect on learnability, but there is no single correct recipe for how to do it. Different classes may order their members differently.

What is important is that each class order its members in some logical order, which its maintainer could explain if asked. For example, new methods are not just habitually added to the end of the class, as that would yield "chronological by date added" ordering, which is not a logical ordering.

Obviously, this is just one style guide, and should not be taken as the only choice; others exist and may work better for your/your team's situation.

Community
  • 1
  • 1
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
4

I use to divide public and private methods and, usually, I put all getters and setters on the bottom of the class. This because getters and setters are the methods less frequently modified so I don't like to scroll all the page to edit other and often, more useful, methods.

By the way, I think that there is no official way to group your methods, same things for your variables.

So the pattern I follow is this:

public class MyClass{


   //variables
   private int number;
   private String name;

   ....

   //constructors
   public MyClass(){ ... }

   public MyClass(String name){ ... } 



   //public methods
   public methodOne(){ ... }
   public methodTwo(){ ... }

   //private methods
   private methodPrivOne(){ ... }
   private methodPrivTwo(){ ... }


   //getters and setters
   public void setName(String name){ ... }
   public String getName() { ... }
}

Here and here you can find two useful answer to your question

Community
  • 1
  • 1
Ema.jar
  • 2,370
  • 1
  • 33
  • 43
  • 1
    I find this style to be very common. – Jazzepi May 24 '16 at 12:59
  • I think this style is a carry-over from C++, because of the "private:" label-like convention. It's also prevalent in Ruby because they do a similar thing as C++. However, if you were to approach Java as a completely new programmer without outside ideas, I think (my opinion) that it doesn't make sense. Imagine you have some internal private recursive method that's called from some public method, they'll be grouped far away from each other. – esotericpig May 12 '21 at 16:32
0

There is no such convention as such in order to define. But it is a good programming practice to put static methods first and then the non-static methods.

Draken
  • 3,134
  • 13
  • 34
  • 54
pramod_m
  • 184
  • 1
  • 9
  • Can you explain why you think that "static methods first" specifically is a good practice? – Andy Turner May 24 '16 at 18:12
  • That's just for readability of the code .. as u can understand so easily and re-engineering becomes so easy. there's nothing more than that – pramod_m May 26 '16 at 10:33
  • 1
    In that case you mean "I consider it", rather than "it is". It is no harder to understand or refactor if the static methods are consistently at the bottom (which *I consider* a better practice, since they're not members of the class - they're just methods which happen to have access to private members of instances of the class). – Andy Turner May 26 '16 at 10:39