DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • A Reference Architecture for Constrained AI-Assisted Design
  • Audit-Ready by Design: Building Lineage, Point-in-Time Reconstruction, and Immutability Into Data Architecture
  • How to Design a Distributed Job Scheduler
  • A Practical Guide to Temporal Workflow Design Patterns

Trending

  • The Three Tiers of Agentic Incident Response: When to Trust AI Autonomy
  • Avoiding AI Debt as a Team
  • Introduction to Error Prone: How to Catch Bugs at Compile Time
  • When a Coding Agent Fixes the Test Instead of the Bug

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

Declaring Final Variables Elegantly Using The Assigner Design Pattern

By 
James Sugrue
·
Mar. 04, 10 · Interview
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
12.7K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

image

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

image

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.

image

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

image

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.

Design

Opinions expressed by DZone contributors are their own.

Related

  • A Reference Architecture for Constrained AI-Assisted Design
  • Audit-Ready by Design: Building Lineage, Point-in-Time Reconstruction, and Immutability Into Data Architecture
  • How to Design a Distributed Job Scheduler
  • A Practical Guide to Temporal Workflow Design Patterns

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook