Over a million developers have joined DZone.

Declaring Final Variables Elegantly Using The Assigner Design Pattern

DZone's Guide to

Declaring Final Variables Elegantly Using The Assigner Design Pattern

· Java Zone
Free Resource

Never build auth again! The Okta Developer Platform makes it simple to implement authentication, authorization, MFA and more in Java applications. Get started with the free API.

A final variable is used to define a constant value and reference, and can only be defined once. In most cases, declaring a final variable is just a matter of assigning to a primitive value or an object reference directly.  However, in some cases, declaring a final class/instance variable may be more involving especially it gets assigned from a method that throws an Exception (eg API for database access, web access), or it may involve multiple statements.

 Editors Note: This content was submitted by geekycoder

In such cases, the code may become cluttered with class helper methods and dummy temporary variables that help define the final variable, making the code look less elegant and harder to maintain. The following common ways of declaring a final variable in those situations might look familiar to many.

Declaring final variable by instance/class method



 Declaring final variable by instance/class variable with static/instance initializer.


The aforementioned ways definitely get the work done but at the cost of elegancy. The helper method and variable help to define the final variable but it inevitably becomes part of class method and variable.


Recommended way: Assigner Design Pattern

A better way to declare final instance/class variable is to use Generic method and interface with the advantages of

  • forcing initializing method in the same statement  as the final variable declaration.
  • reusing as design pattern (term: Assigner Design Pattern) and enhancing code readability

There is generic helper class whose interface and method accept a parametric type similar to the type of final variable.  The advantage of using Generics is that the type mismatch will be caught at compile time rather than runtime. This design pattern is termed Assigner because it assigns value to a variable from initializing method.




Using the design pattern, the code becomes cleaner and elegant.


The above can be downloaded -  assigner.zip (1kb)

Some will probably argue that Assigner Design Pattern may not be efficient compare to the first two ways since additional bytecode classes are generated for Assigner helper class and inner class. However, like other design patterns, code readability and reusability may outweigh the negligible performance loss and inefficiency.

Build and launch faster with Okta’s user management API. Register today for the free forever developer edition!


Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}