Java: Pass by Value, Pass by Reference, and the Five-Legged Wumpus

"Is Java pass by value or pass by reference?" That's often interview question number one.

Of course the Job Interview answer is "Pass by value." That's also the Computer Science answer. However... the Software Engineering answer is "It depends."

If you pass in a Java primitive, such as an int, then setting that field to a different value has no effect outside of the called method.

Same for an immutable object, such as a String or an Integer.

If you pass in a mutable object, say a Wumpus, then you still can't change the address of that object. Setting its reference to a different value affects only within the called method. The original object is not affected. BTW, this includes nulling it out within the called method.

However, if you use that object's setter methods to change its fields, then you have indeed modified that object outside of the called method.

Which brings us to the Wumpus.

public class WumpusTest {

	public WumpusTest(){
		Wumpus wumpus = new Wumpus();

	private void modifyWumpus(Wumpus wumpus){
	public static void main(String... args){
		new WumpusTest();		
	//Inner class
	class Wumpus{
		void setCountLegs(int nCountLegs){
			m_nCountLegs = nCountLegs;
		void displayCountLegs(){
			System.out.println("The Wumpus has nCountLegs<"+m_nCountLegs+">");
		private int m_nCountLegs = 4;
	//End inner class

Run this. First the Wumpus has four legs; then it has five legs.

Disconcerting to the Wumpus; but maybe useful for scratching Wumpus ears.

Why this is useful knowledge for the Software Engineer is:
  • Avoid unintended side effects. ("First, do no harm.") Don't think you can set something within a method and not have it affect an external object.

  • Avoid redundant code. You don't have to pass the object's reference out as the return value for the called method. The object already is changed.

  • Avoid ineffectve code. Setting the field wumpus to a new instance of Wumpus has no effect on the original Wumpus.
I thank you and the Wumpus thanks you for your attention today.


It's been over four years since I started at MIT Lincoln Laboratory, and I've learned a lot.

To comply with the spirit as well as the letter of their information management policies, you won't see anything specific here at all.

What I can say, however, is that I've developed one hell of a skill set in:

  • Core Java
  • GUI, especially Swing
  • Componentization
  • Large scale data visualization
  • Networks
All readily transferrable skills.

And while the salaried staff have to attend meetings and do a lot of PowerPoint, I get to be what my boss calls "a coding machine."

It's been a very good four years.


New Worlds to Conquer in User Interfaces

Audio user interfaces, voice-controlled user interfaces, color-blind user interfaces, restricted-motion user interfaces, restricted-cognition user interfaces: I'm learning a lot building computer games which are accessible to the specially challenged.

This wasn't the original idea. In fact, the games are designed for the general casual games market.

But somehow along the way, the accessibility thing seems to have taken over. Maybe it was my years working at Kurzweil. Or maybe it's the fine folks I've met at the various accessibility venues.

In any event, it's making me a much stronger user interface developer.

I'm getting experience with usability issues such as focus traversal, information bandwidth, and event timing. I'm also mastering a lot of neat technologies like speech synthesis and voice-recognition.

As a result, my non-game user interfaces are getting a lot better, and I seem to be building them faster.

What's Hot and What's Not

Tiobe Software publishes a survey of programming languages, rated by popularity.

In this case, popularity is defined by: "worldwide availability of skilled engineers, courses, and third-party language-specific tools." Tiobe notes: "The popular search engines Google, MSN, and Yahoo! are used to calculate the ratings. Observe that the TPC index is not about the best programming language or the language in which most lines of code have been written."

By Tiobe's rating, Java is rated the most popular language, double that of C++. Java is getting more popular; C++ is getting less popular. C# is getting more popular.

This is consistent with my own observations of the Boston job market. The open C++ jobs are diminishing. The open Java jobs are increasing.


Mom got me interested in poetry young. Here are some of my favorite


Link to: