Placement of instance variable declarations

I've seen some developers put instance variable declarations at the end of classes though I mostly see them placed at the top. The only reasons I can think of for doing this are stylistic preference or maybe it somehow makes them easier to work with in an IDE. Is there a more legitimate reason for choosing this style?


Asked by: Paul310 | Posted: 28-01-2022






Answer 1

Because of "Program to an 'interface', not an 'implementation'." (Gang of Four 1995:18) (http://en.wikipedia.org/wiki/Design_Patterns#Introduction.2C_Chapter_1), some people prefer to declare instance variables at the bottom of the class. the theory being that the user of a class is more interested in what they can do with the class(methods) as opposed to how something is done(variables). Placing the methods at the top of the class exposes them first to the user when they look at the code.

Answered by: Steven313 | Posted: 01-03-2022



Answer 2

There is no particularly "good" reason for doing it one way or another. The only thing that really matters is that everyone on the same project does it the same way.

However, placing them at the top is far more common in my experience, and is the practice recommended by the Java style guidelines, so that's what I'd go with.

You can enforce your chosen convention with an automatic source code formatter such as Jalopy, or that which comes with Eclipse.

Answered by: Aida910 | Posted: 01-03-2022



Answer 3

It's mostly (if not entirely) personal preference. I like them at the top, but I couldn't really give a better reason for it then that it's the way I'm used to.

Answered by: Steven664 | Posted: 01-03-2022



Answer 4

Most instance variables are private, so I tend to put them at the bottom because I declare members in order of decreasing visibility. If I declared them in order of increasing visibility they would be at the top, which is also reasonable.

What I don't like is having private fields followed by public fields followed by private methods. If I am developing a client class I want all the public parts together (since that is all I am interested in.)

Answered by: Byron758 | Posted: 01-03-2022



Answer 5

I've always rationalized that if you have a private class level variable, then you've either hardcoded a configuration, or you're tracking state one way or another. If you're tracking state, then 1) that should be made obvious to everyone who's going to write code in the file from the moment they open the file, and 2) tracking lots of states is a huge code smell, and if my devs are doing that, then I want it to be obvious. So putting it at the top, imho, makes bad code more obvious, and serves as a warning for future people who edit the class.

Definitely separate your public/protected vs private fields and members though, because people interested in one, likely aren't interested in the other.

Answered by: Maya539 | Posted: 01-03-2022



Similar questions

c# - Is there a library that generates UIs based on metadata declarations like this>>?

Do you know about a library that allows us to generate UI by just stating that it should be generated? I think there must be a person who have implemented a mechanism allowing us to transform code like this: class Main { @Command int add(int a, int b) { return a+b; } } into, say, a dialog with 2 text fields and a button? Or into a webform? You've got the idea, r...


java - Generic wildcards in variable declarations in Scala

In Java I might do this: class MyClass { private List<? extends MyInterface> list; public void setList(List<MyImpl> l) { list = l; } } ...assuming that (MyImpl implements MyInterface) of course. What is the analog for this in Scala, when using a Buffer? import java.lang.reflect._ import scala.collection...


java - Why is the order of declarations important for static initializers?

I have this code private static Set<String> myField; static { myField = new HashSet<String>(); myField.add("test"); } and it works. But when I flip the order, I get an illegal forward reference error. static { myField = new HashSet<String>(); myField.add("test"); // illegal forward reference } private static Set<Strin...


Java Syntax: Shortcuts in Method Declarations?

Quickie pseudo-aesthetics question: For field declarations, the following is allowed: int i, j, k; providing three variables for integers. Does anything similar exist for method declarations, ala: public int getI() {/* ... */}, getJ() {/* ... */}, getK() {/* ... */}; so that the method accessibility, return type, etc don't have to be redundantly s...


java - Struts form and variable declarations

I have a question and I hope somebody can help me out here. Well, I'm developing an application with Struts 1.3.10 and I have a struts form with an object that contains a property. That property is declared as the primitive type int and the problem comes when the app shows to the user zeroes(0) instead of nothing when I retrieve that data from the database and turns out to be NULL. Have any of you experienced ...


java - JAXB appending unneeded namespace declarations to tags

I'm implementing a homebrew subprotocol of XMPP, and i'm using combination of StAX and JAXB for parsing/marshalling mesages. And when I marshall a message I end up with loads of unneded namespace declarations: <ns2:auth xmlns:ns2="urn:ietf:params:xml:ns:ilf-auth" xmlns:ns4="ilf:iq:experiment:power" xmlns:ns3="ilf:iq:experiment:init" xmlns:ns5="ilf:iq:experiment:values" xmlns:ns6="ilf:iq:exper...


java - Duplicate namespace declarations in JAXB generated XML

I am using JAXB to generate XML from Java objects, it's a realtime, quite high message rate application and works fine most of the time. However occassionally and without any obvious clues as to why, I am getting duplicate namespace declarations in the generated XML. eg: <UpdateRequest xmlns="http://xml.mycomp.com/ns/myservice" xmlns="http://xml.mycomp.com/ns/myservice"> <fiel...


Java: Multiple class declarations in one file

In Java, you can define multiple top level classes in a single file, providing that at most one of these is public (see JLS §7.6). See below for example. Is there a tidy name for this technique (analogous to inner, nested, anonymous)? The JLS says the s...


java - getting variable declarations

i created a parser of ASTParser type for a compilationunit. I want to use this parser to list all the variable declarations in the functions present in this particular compilationunit.Should i use ASTVisitor? if so how or is there any other way? help


Java: Forward declarations of classes in namespaces

How do I do forward declarations in Java? I have two classes, each needs to call a method in the other and they both reside within different namespaces. For example... package one; class A { public void foo() { B b = new B(); b.bah(); } } and package two; class B { public void bah() { A a = new A(); a.foo(); } }






Still can't find your answer? Check out these amazing Java communities for help...



Java Reddit Community | Java Help Reddit Community | Dev.to Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)



top