Archive

Posts Tagged ‘Shadowing’

Shadowing Variables in Java Demystified

One of the meanings of the word “Shadow” in the Oxford Dictionary is “a weak or less good version”. Shadowing in Java is also something similar. One can shadow a variable in several ways. I would try to describe the one most comman ways which would trip most of us i.e. “Hiding an instance variable by shadowing it with a local Variable”. What exactly is Shadowing in Java? Shadowing is nothing but redeclaring a variable that’s already been declared somewhere else. The effect of Shadowing is to hide the previously declared variable in such a way that it may look as though you’re using the hidden variable, but you’re actually using the shadowing variable (The Code Snippet below will make it more clear). This most of the times happens by accident and causes hard-to-find bugs.

Read more…

Advertisements
Categories: Java Basics Tags: , , ,