am wondering, what is the right way to organize the members of a class in java? Should we start with the public members and then the private or the private members first?
Asked
Active
Viewed 287 times
0
-
See this answer: http://stackoverflow.com/a/5210844/647772 – Nov 12 '12 at 11:37
-
There isn't a mandatory norm for that. You can do what you want. But I always use this order for better clarity: Attributes - Constructors - Destructors - Public functions - Private functions – Napsteur Nov 12 '12 at 11:37
-
possible duplicate of [which order is better for field declaring](http://stackoverflow.com/questions/11791861/which-order-is-better-for-field-declaring) – assylias Nov 12 '12 at 11:38
-
@Napsteur You *can* but you *shouldn't*. Code is read 80% of the time. Don't make it harder than necessary for the reader. – Nov 12 '12 at 11:39
-
1@Napsteur.. And there is no such thing as `Destructors` in Java. – Rohit Jain Nov 12 '12 at 11:41
-
@Rohit: there is destructors. The syntax is `public void finalize(){}` – Napsteur Nov 12 '12 at 11:46
-
2@Napsteur.. [They are not](http://stackoverflow.com/questions/171952/is-there-a-destructor-for-java) <-- Click.. Also see: - [finalizers - wiki page](http://en.wikipedia.org/wiki/Finalizer) – Rohit Jain Nov 12 '12 at 11:49
-
See the [Java Code Conventions](http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-141855.html#3043), especially '3.1.3 Class and Interface Declarations'. – Nov 12 '12 at 11:38
-
Some parts of those conventions, written 15 years ago, seem a little outdated... – assylias Nov 12 '12 at 11:39
-
For example: http://nurkiewicz.blogspot.co.uk/2012/10/java-coding-conventions-considered.html it is a bit of a rant but makes a few valid points. – assylias Nov 12 '12 at 11:41
-
@assylias - He makes a couple of valid points (indentation levels and text width), but he degenerates into irrelevant nitpicking of the examples ... and I got fed up. Yea, the old Sun document could be updated, but I don't think there is anyone left with the "standing" to make it happen. (New coding guidelines are only going to be useful if it is widely accepted.) So I recommend that people follow the original guidelines tweaked / relaxed to fit in with current practices. – Stephen C Nov 12 '12 at 11:54
-
@StephenC Fair enough. [This answer](http://stackoverflow.com/a/11792228/829571) makes good points too. – assylias Nov 12 '12 at 11:57
1 Answers
1
Member is an interface that reflects identifying information about a single member (a field or a method) or a constructor.
(Oracle official documentation)
So I would suggest that every time it's possible you should make your fields
private and make setters and getters
for them. As for the methods - am really not sure if you have to organize them on the fact if they are public
or private

Leron
- 9,546
- 35
- 156
- 257