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
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • How Large Tech Companies Architect Resilient Systems for Millions of Users
  • Building Security into the Feature During the Design Phase
  • Build a Scalable E-commerce Platform: System Design Overview
  • A Step-by-Step Guide to Write a System Design Document

Trending

  • SQL Server Index Optimization Strategies: Best Practices with Ola Hallengren’s Scripts
  • A Modern Stack for Building Scalable Systems
  • From Fragmentation to Focus: A Data-First, Team-First Framework for Platform-Driven Organizations
  • Key Considerations in Cross-Model Migration

Declaring Final Variables Elegantly Using The Assigner Design Pattern

By 
James Sugrue user avatar
James Sugrue
DZone Core CORE ·
Mar. 04, 10 · Interview
Likes (5)
Comment
Save
Tweet
Share
12.2K Views

Join the DZone community and get the full member experience.

Join For Free

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

  • How Large Tech Companies Architect Resilient Systems for Millions of Users
  • Building Security into the Feature During the Design Phase
  • Build a Scalable E-commerce Platform: System Design Overview
  • A Step-by-Step Guide to Write a System Design Document

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!