Programming Tutorials Browser Tutorials Articles Struts Tutorials Hibernate Tutorials

  Tutorial: Shadow variables are evil

The inner variable shadows the outer variable.

Tutorial Details:

Methods and inner classes are declared in other classes. This allows the inner structures to reference variables in the outer structure. This is very useful, except for one dangerous case. When the inner structure, eg a method, declares its own local variables, what happens if a local variable has the same name as an outer instance variable?
The inner variable shadows the outer variable. This is almost always a bad idea, and leads to a lot of confusion. My advice is never shadow a variable.

Setter methods
The one case where this is commonly used is in setter methods. These are methods which simply set instance variables. The reason to make them methods rather than simply public variables is so that in new value can be checked or the implementation may later be changed, and this change can be hidden in the method. However, the majority of these methods simply pass a value directly to/from an instance variable.


 

Rate Tutorial:
http://www.roseindia.net/java/java-tips/data/variables/60shadow-variables.shtml

Read Tutorial at: Click here to view the tutorial

Rate Tutorial:
Shadow variables are evil

View Tutorial:
Shadow variables are evil

Related Tutorials:

Container support for objects
Container support for objects
 
Java Tip 81: Jazz up the standard Java fonts - JavaWorld
Java Tip 81: Jazz up the standard Java fonts - JavaWorld
 
Achieve strong performance with threads, Part 2
Achieve strong performance with threads, Part 2
 
Smartly load your properties
Smartly load your properties
 
replacing implementation inheritance by contract inheritance
replacing implementation inheritance by contract inheritance
 
confusing title
confusing title
 
Good ideas
Good ideas
 
Fixing the Java Memory Model, Part 2
Writing concurrent code is hard to begin with; the language should not make it any harder. While the Java platform included support for threading from the outset, including a cross-platform memory model that was intended to provide \"Write Once, Run Anywh
 
Flexible User and Environment Ant Configuration
Flexible User and Environment Ant Configuration The de facto standard for building, packaging, and deploying Java applications is Apache Ant. Small differences in developers\' environments or preferences may cause problems with some Ant tasks that invo
 
JEP - Java Mathematical Expression Parser
JEP - Java Mathematical Expression Parser JEP is a Java API for parsing and evaluating mathematical expressions. With this library you can allow your users to enter an arbitrary formula as a string, and instantly evaluate it. JEP supports user defined
 
Jython
Get to know Jython, in this first article in a new series introducing alternate languages for the Java Runtime Environment, alt.lang.jre. Jython is an implementation of the popular scripting language Python, but running on a JVM. For Python developers Jyt
 
Designing J2EE Applications for Real-Life Clustered Environments
Designing J2EE Applications for Real-Life Clustered Environments In this article, the authors draw from their practical experience to list and discuss some critical considerations when building J2EE applications so that they can be deployed in a cluste
 
The JModalWindow Project
This article explains the workings of the JModalWindow project, which provides two top-level components, called ModalWindows, that introduce such modality.
 
Understanding the Interplay Between Utility Classes and Static Initialization
Java is an OO language, which means much of the functionality of a Java application is encapsulated into cohesive classes that can be instantiated and acted upon.
 
When tears bring you back your beloved method...
Accessing platform-specific information hasn't always been easy. While you could certainly create processes with Runtime.exec(), dealing with differences across platforms to build parameter sets often led to headaches. In addition, the getenv() method of
 
SeSAm - Shell for Simulated Agent Systems
Multi-Agent Simulation Environment SeSAm (Shell for Simulated Agent Systems) provides a generic environment for modelling and experimenting with agent-based simulation. We specially focused on providing a tool for the easy construction of complex models,
 
JDock 1.1 - Swing docking framework
JDock is a pure java swing framework for managing, moving and resizing inner windows or components using a layout manager like a BorderLayout or a GridBagLayout.
 
JavaRSS.com 2004: Review of the Year
A look back at the major events of 2004 in Java.
 
Introduction to JSP Declaratives Declarations
Introduction to JSP Declaratives Declarations INTRODUCTION TO JSP DECLARATIVES Syntax of JSP Declaratives are: <%! //java codes %> JSP Declaratives begins with <%! and ends %> with .We can embed any amount of java code in the JSP Declaratives.
 
Introduction to JSP Scriptlets
Introduction to JSP Scriptlets INTRODUCTION TO JSP SCRIPTLETS Syntax of JSP Scriptles are: <% //java codes %> JSP Scriptlets begins with <% and ends %> .We can embed any amount of java code in the JSP Scriptlets. JSP Engine places these code
 
Site navigation
 

 

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2006. All rights reserved.