Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java declarations (ordering)

In Java, what's generally the most accepted way to organize a class in terms of the order in which declared data members and methods should be listed in the class file, keeping in mind the following and anything else you can think of for each one:

  • its visibility
  • whether it's a constructor, method, or member
  • if it's a method, does it overload, or override other method(s)?
like image 897
Cam Avatar asked Oct 23 '25 21:10

Cam


1 Answers

I've found that the conventions in Code Conventions for the JavaTM Programming Language to be quite suitable. Chapter 3 answers this question.

Mostly it ignores visibility or whether it is an overloaded function etc.

It is simply in order:

  • Class (static) variables
  • Instance variables
  • Constructor
  • Methods

A more rigorous approach could be taken, but I do not see a strong argument in its favor.

like image 126
Kris Avatar answered Oct 25 '25 10:10

Kris