Compact Attribute Declaration In Java

Attributes in a class don't have to be declared in the verbose way:

public class VerboseClass {
    private String firstName;
    private String lastName;
    private String description;

Attributes of the same type and visibility can be declared in a more compact manner:

public class CompactClass {
    private String firstName,lastName,description;

This works in most cases - except you want to annotate each attribute in different way....

NEW: Online Workhop Effective WebApps without Frameworks is also coming to: MUC Airport.

Airport MUC workshops: Web (SPA, PWAs, Offline, Desktop, Mobile) Applications Essentials and Effective Web Applications. No migrations. #usetheplatform

Podcast: and newsletter:

A book about rethinking Java EE Patterns


Even more compact:
public class CompactClass {private String firstName,lastName,description;}

What is the argument?

Posted by Stefan Bley on April 09, 2010 at 03:01 PM CEST #


There is no argument. At my C++ time, I used the compact style of declaration. In Java I tend to use the verbose one. For DTOs (if you really need them), the compact style would be more appropriate - they are bloated any way...

Posted by Adam Bien on April 09, 2010 at 03:47 PM CEST #

Hi Adam,

In my opinion the bloated stuff is not the attribute declaration it's the JavaBean style getter and setter declaration.

I hope this will change to somewhat like in C# in a next Java release. See my blog:

Kind Regards,

Posted by Simon Martinelli on April 09, 2010 at 04:00 PM CEST #


you are very right, see: (the post is 4 years older, than yours :-)).

I only noticed, that I tend not to use the compact declaration - and wrote the post to remember the another option. The compact style is not used in Java a lot - what is interesting...

thanks, as always, for your comment!,


Posted by Adam Bien on April 09, 2010 at 04:27 PM CEST #

Why not address something easier to solve like the one true placement of braces or how to achieve peace in the middle east?

So, Reductio ad absurdum:

public class C{private String f,l,d;}

See how much "bloat" I've saved!

What is achieved? It's harder to maintain and grok in the first place. Are those your goals?

Posted by Gene De Lisa on April 09, 2010 at 04:31 PM CEST #

This compact style of declaration is widely known as bad style, isn't it? But lately I'm getting to like bad style in some scenarios. DTOs is one case. What about making the attributes even public to get access through simple dot notation, like verboseObject.firstName? This would also make the clients more compact.

Posted by Nick Wiedenbrück on April 09, 2010 at 04:36 PM CEST #


why it is bad practice? Providing the same information (private String) is just redundant. The verbose style, however, is more readable.

Btw. I would have nothing against public attributes in DTOs. It should be a conscious and consequent decision...

thanks for your feedback!,


Posted by Adam Bien on April 09, 2010 at 04:44 PM CEST #

Could be worthwile to mention the immutable, with public fields, value object as well here as the getter/setter pattern annoyances are mentioned. Immutable is nice too.

public class CompactClass {
public final String firstName; ...

public CompactClass(String firstName, ...) {
this.firstName = firstname;

Posted by Mattias Bergander on April 09, 2010 at 04:45 PM CEST #


I only removed the redundant information - you removed actually interesting information :-).

Why to keep redundant information in source? One reason would be readability - but that is a matter of taste.



Posted by Adam Bien on April 09, 2010 at 04:47 PM CEST #


good stuff - will write a dedicated post about that. Thanks for your suggestion!,


Posted by Adam Bien on April 09, 2010 at 04:56 PM CEST #

For a compact style to work, javadoc needs to be fixed. As it stands now, if I do declaration:
/** debug levels */
int ERROR=0, INFO=2, DEBUG=4;
javadoc will generate description for "int ERROR=0" and all other will be empty.

Posted by Igor on April 09, 2010 at 08:46 PM CEST #

And I completely agree that setters/getters bloat need to be fixed, but do not think it should go C# way.
Much more efficient would be declaration like this:
@property private String name;
for which compiler would automatically generate public get/set duple. For the quite rare cases when read-only or write-only or special get/set is required people would just use current way things are done.

Posted by Igor on April 09, 2010 at 08:55 PM CEST #

@Igor: You described an enum, which can perfectly be described with JavaDoc ;-)

Posted by Marcus on April 10, 2010 at 12:52 AM CEST #

Declaring the fields on multiple lines in multiple declarations can also aid version control and merging - less collisions on the same line?

Posted by James Barrow on April 11, 2010 at 11:47 PM CEST #

Code convention is dead?

"One declaration per line is recommended since it encourages commenting"

Posted by Joel Lobo on April 19, 2010 at 10:34 PM CEST #


right. I also prefer the verbose syntax - and forgot about the compact one:-).

Regarding JavaDoc - I would only comment, what really provides added value - otherwise it could be even a smell:

thanks for your feedback!,


Posted by Adam Bien on April 22, 2010 at 02:41 PM CEST #


I did some experimentation with this, but be careful:
You will start you development using:
x = myObject.publicFinalField;

Then you may want to serialize myObject to XMl using JAXB for example. And then you realize that JAXB is not compatible with final field ...

You go back to setters and getter and you have to change your code all around.

A great thing about Scala is that it would not be a problem, since they have a Uniform Access:


Posted by Bruno on August 26, 2010 at 01:07 PM CEST #

Post a Comment:
  • HTML Syntax: NOT allowed
Online Workshops
...the last 150 posts
...the last 10 comments