Variable shadowing

Today we’ll look into a rare feature of Java: variable shadowing

First, let’s define what is a shadowed field or method:

A field is considered shadowed when

  • a subclass of its declaring class declares a field with the same name and same
  • a variable having the same name and type is declared in the local scope
  • a method argument/parameter is declared with a same name and type

Read more of this post

Final variable in Java

This short article introduces the definition and usage of final variables in Java and some interesting use cases.

Read more of this post

Object immutability in Java

I’ve seen many developers struggling with subtle mutability issues in Java. This article will expose the major gotchas of object immutability.

Read more of this post

ThreadLocal explained

Today we are going to look into the ThreadLocal internals, its usage and all the related pitfalls.

ThreadLocal, among other Java advanced features like volatile variables or nested classes, is rarely used.

The main reason is its relative complexity and mostly the very few cases where ThreadLocal is really required.

Yet, if its underlying mechanism is well understood, ThreadLocal can be very handy in some situations. No wonder it is at the heart of some framework features, Spring
TransactionSynchronizationManager for example.
Read more of this post

Java, pass by value/reference or copy of value/reference ?

This basic question is in any Java 101 courses and the answer is quite obvious. But in fact things are a little bit trickier than they seem to be.
Read more of this post