Error 51: Unable to communicate with the VPN subsystem

October 6th, 2009

I upgraded Mac OS X to 10.5.8. I did the upgrade after my hard drive died and imported my profile from an old backup. When I tried to run my Cisco VPN client that I use for work, I was given this error:

Error 51: Unable to communicate with the VPN subsystem

First, I read that I could do this in the terminal:

$ sudo /System/Library/StartupItems/CiscoVPN/CiscoVPN restart

Since I don’t have a CiscoVPN directory under startup items, that didn’t help me. Then I read that you need to to repair disk permissions using disk utility. I did that and it found several inconsistencies. Good stuff, though, it also did not solve my problem.

Then, I read that I could do this:

$ sudo SystemStarter restart CiscoVPN

The command didn’t produce any output, nor did it fix my problem.

Finally, I came across a site that told me to do this:

$ sudo kextunload /System/Library/Extensions/CiscoVPN.kext
$ sudo kextload /System/Library/Extensions/CiscoVPN.kext

This finally fixed my problem. Here is the sequence of commands that I ran and the output:

$ sudo kextunload /System/Library/Extensions/CiscoVPN.kext
kextunload: unload kext /System/Library/Extensions/CiscoVPN.kext failed
$ sudo kextload /System/Library/Extensions/CiscoVPN.kext
kextload: /System/Library/Extensions/CiscoVPN.kext loaded successfully
$ sudo kextunload /System/Library/Extensions/CiscoVPN.kext
kextunload: unload kext /System/Library/Extensions/CiscoVPN.kext succeeded
$ sudo kextload /System/Library/Extensions/CiscoVPN.kext
kextload: /System/Library/Extensions/CiscoVPN.kext loaded successfully

Hopefully, this will save someone some time.

The Lego Game at Agile Atlanta Users Group

April 15th, 2009

When I joined Thoughtworks, I went to India for training and played the Lego game as a way to experience a form of Agile product development. It was a lot of fun and a great experience.

Almost 3 years later, I got to help facilitate the game for the Agile Atlanta Users Group. Only this time, I got to be one of the customers. It was a blast.

In case you aren’t familiar with the Lego game, the objective is to form a team of people and have them go through 3 agile iterations developing a lego creature via story cards.

Tim Kaddon was one of the people subjected to to our lego demands and wrote a great blog entry about his experience in the game.

Thanks to Conrad Bentham for organizing and facilitating the session, and Adrian Wible, Lefak Fakiyesi, and Brian Gunthrie for participating and making it fun.

Top Highlights:

Seeing how seriously people take building anything, including a lego animal.

Seeing the big Aha! moments, like where they realized they forgot to ask the customer what they wanted, or what the priorities were in the first iteration.

Me trying to find a balance between getting the lego animal I’m satisfied with, and torturing the team in order to make the game more interesting

Being reminded of how great a forum the game is for communicating a wide variety of Agile concepts.

Some tips on The Thoughtworks Interview Process

March 20th, 2009

I recently checked the statistics for my blog and I was surprised to find that one of top reason that someone came to my blog was because I had tagged a previous article with the “Thoughtworks interview process” or something to that effect. Unfortunately for those people, the article was brief, and didn’t reveal much more than the Thoughtworks interview info page.

Given that I have been involved in the interview process at thoughtworks, I thought I would share my perspective for anyone who’s interested - the perspective of a developer at Thoughtworks that may be interviewing you at some point.

General Considerations

Here are some general consideration that go through my mind when I’m evaluating a candidate.

1. Are you a top notch developer
2. How much do you know about Agile, and do you have experience in Agile
3. Are you a fit for consulting - ie: can you travel, will you be mesh well in a variety of client environments
4. Are you generally interested in Technology
5. Will we want to work with you, are you fun to work with and would I learn from you
6. Are you poly-skilled - will you be able to work in many programming languages, platforms, and frameworks
7. Are you open to learning / are you open to new ideas

Specific Tips

1. Spend some time researching us and have an idea what we care about. This doesn’t mean you have to know a detailed history of the company, but you should have an idea of some higher level points of who/why/what we are.

2. Take the code submission seriously. This is the one of the first major gates that needs to be passed to get to the interview process. It’s your chance to express to us your coding philosophy. Don’t communcate “I don’t care about this part” or “I didn’t have time to do it well.”

3. Don’t try to tell us what you think we want to hear. Don’t try to show that you have more knowledge/experience than you do; it’s very easy to spot that. Be yourself, honestly express what you know and don’t, and what you are interested in, or not.

4. Have fun. We hope to have fun on days were we interview people, and it’s great when the candidates have a good time as well.

5. Get a good nights sleep before the day of the interview. It’s going to be a long day, and you don’t want to try to swing it on 4 hours of sleep.

If you don’t get in, don’t worry

1. There are many points of failure - Our process is not easy. Unlike the way many other companies interview developers, candidates don’t just show up and talk to a few tech-lead or architect people. This means that if you have an “off day” there are more stages that it can affect.

2. Our process has variation - There is no perfect pass/fail criteria in the process. It depends on a myriad of factors that might be different on a different day.

3. Perhaps you just weren’t a good fit overall And that’s okay. No one is able to work anywhere. You are probably exactly what someone is looking for.

4. You might not be ready yet, but that doesn’t mean that you won’t be next time around. Sometimes, there are people that have the right background, and the right attitude, but they are a little short on experience or skill-sets.

Finding out if the game can be won

February 8th, 2009

I was playing a video game the other night and I thought of another characteristic about how to build a successful software development project. I know, relating video games to multi-million dollar software projects may be laughable to some, but bear with me.

I had blogged previously about how to win the game where I discuss the concept of game theory as it applies to software development. I outlined what I considered the highest level considerations, but I wanted to go into some more depth into how you become good at a game.

There is a particular quality to games to determine if you are winning them or not; When the game allows rapid cycles and fast feedback, you can figure out whether or not you can get better at the game. In the case that you aren’t getting better, you probably should not be playing the game, or you should change the game so the player can get better at it.

The last point is important, and one of more difficult considerations. Perhaps that team will never win the game. The only way to discover that is to play the game in cycles. These cycles provide the information needed. In the previous blog post, I outlined that there were 3 important aspects of a game.

1. Knowing how to win the game
2. Being motivated to win the game
3. Having the ability to win the game

This is all fine in theory, but how do you really know whether a team has unified these three considerations?

Back to my analogy to video games. I was playing a racing game, and at first, it seemed like there was little I could do to beat the computer players. The racing tracks were too hard, the computer players were too good. There was too much chance involved. Then, as I continued to play, I became a little better. Energized by the awareness of this, I played more, and got even better. After a while, I beat the game.

How did I know that I would get better? It certainly seemed like I was failing at first. The answer is that there were rapid cycles of practice and feedback. I got information about how I was doing.

How does beating a video game apply to the 3 principles that listed above?

The first is easy; I had to complete levels of the game that incrementally led to my winning the game. I clearly knew what it took to win the game. Each level that I won was not only a discrete step towards winning, but it was a test of my ability to progress to the end goal. I see a parallel here to having stories represent cohesive unit of valuable functionality or features.

The second is also somewhat easy; I wanted to win. I enjoyed playing the game. It was stimulating and rewarding enough for me to continue trying to win. However, I think it’s important to note that a large part of the reason that I maintained motivation was that I knew I was getting better, and I knew that the end goal was achievable. If I can’t win a game, I lose interest. If I were forced to play a game that I felt was losing, I would also lose motivation, even if you were paying me to play the game. This is reminiscent of a project death march.

The third point is perhaps the most relevant to this blog post. There would be no way to know if I was capable of winning the game without short, achievable, and valuable cycles that informed me of my progress. Not all games can be won. If you were to have me play a computerized chess game meant to compete with a chess grand master, I may never win, even if I play for years.

A difficult part of a project is determining if the people of the project have the ability to win the game. There are many reasons why the game is setup such that it can’t be won. It’s not just whether the people involved aren’t capable (wrong skill sets, insufficient experience, etc), it’s often because the structure of the project impedes success and/or the scope is too large.

I have a lot more to say on this topic. I think that the game theory analogy is very applicable to software development, and organizational behavior in general. I think that there is a fundamental project management question; How does one form a group of people to achieve any endeavor? The general answer to this question is applicable to software development as well.

Finding tests related to code you are changing

December 7th, 2008

Often, when working on a well tested codebase, you find that you want to know what tests may exist that test the code you are going to modify.

Often a quick search on usages of a method or class is sufficient. You look at the tests briefly to see what is going on, and then you make a new test to assert the new behavior that is going to be added.

Sometimes, when there are a lot of tests, I just prefer to remove the code I’m going to modify, and see what tests fail.

This technique provides some quick and useful information. For instance:

  1. What tests were written that test the code? Are there any applicable tests?
  2. Are there other tests that implicitly test the code and should not?
  3. Are the current tests testing all the conditions?
  4. Is there a test that no longer applies?
  5. Is there a test who’s expectation are changing? That test can be modified instead of writing a new test.

This approach tends to be practical in the case of unit tests, and perhaps a few integration tests that you may know are related. Otherwise, it becomes too expensive to run a long running suite.

An additional benefit is that if you already know of some tests that should be testing some code, you may find a test that meant to test the logic, but for some reason was always passing.

Winning the Game

November 16th, 2008

I was talking to a fellow Thoughtworker about the challenges of consulting, and I thought that one of his points was interesting. What he basically said is that you have to try to understand the motivations behind all the stakeholders in a project; not everyones primary goal is delivering software, there are many motivating factors that drive people. A good consultant will try to understand what their motivations are and then try to unify their goals with successful software delivery.

This made me think about a general organizational “smell” in software development. For lack of a better label, I’ll call it fragmentation of goals. A manager might be juggling multiple projects, of which yours is not the highest priority. QA’s may be measured based on how many bugs they find, or how many test cases the document. A developer might see a better chance at promotion by going to all the meetings he or she is invited to, moreso than the delivery of features. Entire teams within a project may even be motivated by goals that are not complimentary to the success of the project.

When I was in college, I liked to compare the team projects I was on to games. My goal was making sure that we were winning the game (in that situation, getting an “A”). Later in my education, I read Agile Software Development by Alistair Cockburn, and he made the analogy that software development is like a cooperative game that we are trying to win. I’ve always loved that analogy, and I think that it’s a useful tool in conceptualizing an effective team or project. Much like a team game, software projects must be a coordinated and unified effort amongst individuals to achieve a common goal.

There are three important factors that are core to a team being able to deliver software successfully, and each easily becomes fragmented (I’m generally using team and project synonimously).

  1. Knowing how to win the game
  2. Being motivated to win the game
  3. Having the ability to win the game

I’ll discuss each inline below.

Knowing how to win the game

A team or project must know how to “win the game” - there has to be acceptance criteria on the results of the teams efforts. Not having this clearly established makes it impossible for people to unite towards winning (I am not implying having all the “requirements” - I am talking about a clear understanding of how to accomplish the business goal). Without a clear goal, a team can only work towards something that they hope is right, and everyone may not agree on what that is.

An important aspect of this strategy is to have a single dedicated product owner - someone who can provide a vision of the end goal, and be the final decision maker for what aspects of the product will achieve that goal. This will help not only knowing what to build, but have the ability to prioritize when to build something.

Being motivated to win the game

The second part is ensuring that the team is motivated to win the game. The team as a whole will suffer if the individuals are engaging in activities meant to promote their own goals over the goals of the team. There are many, many parts to this. My basic opinion is that measuring an individuals success should be based on their contribution to the team, and the teams success.

It is difficult to make sure that everyone has the incentives to be motivated. It is not enough to hire people and then put them to work on something. You must make sure that they are happy, and personally interested in succeeding. It’s especially important to make sure that the personal goals and desires of the individuals aligned with the success of the team.

This doesn’t just apply to individuals. Often large projects involves dividing the project into smaller teams. It’s not beneficial to set up teams with goals that allows them to succeed while the project fails. The concept that this works is often an attempt to suboptimize a system - the idea that if the individual components are succeeding then the whole will succeed. This approach is flawed and often leads to a team achieving it’s goals, even at the expense of the project.

Having the ability to win the game

The team must have the ability to be effective and able to win the game. Further, the team must be empowered to make changes to increase their ability to win the game. The easy example of a team not being able to win is that when the team is formed with people who’s skill-sets are not suited to success. For example, building a team of people that have never done web development, and asking them to build a sophisticated web application. Another easy example is creating unachievable goals for the team, such as having the scope be too great for the team to ever achieve.

More complicated scenarios involve putting a restriction on a team that they must use a certain language or technology, or to disallow some tools and technologies, without consideration as to whether it actually helps the team succeed. It could even be as simple as the developers not being able to get licenses for software that they need, or computers to support the development efforts.

This point is part of the reason that it is critical for teams to be responsive, continually improving, and honest about what will make them more effective and better able to succeed. It could be that 1 month into a project, the team already knows that it’s failing and it’s likely that nothing can be done to help. It could be that the team is producing features, but is losing a lot of time trying to communicate to disparate groups that they rely upon.

Designing the game

A final point here is that all of these factors are part of setting up the game in the first place. Often the decisions that are made early on in a projects life sets it up to succeed or to fail. In my experience, the structure of a software project tends to be an extension of the organizational behavior of the company. That structure may not be an ideal structure for a software project.

Regardless of how the project begins, it’s important to change how the game is played if it is necessary to win the game.

When will we have a real multi-user windowing system

October 28th, 2008

I once worked on a VNC type application, where the purpose was to allow some number of users to share any desktop application to a group of people (part of the allure is that windows applications could be shared to Linux users). The features that were meant to set it apart from the typical desktop sharing application like VNC was that it would enhance collaboration amongst the users.

It was a fun application to write - It had a Java Swing front end with JNI to use portions of the win32 API and X-Windows API, and we wrote a video and audio server to distribute the view of the application and allow the users to talk to each other in on online group phone call while they were using the application.

There was one desired feature that we could not implement. We wanted to allow users to simultaneously enter text, and simultaneously control their own mouse cursor to interact with the application. We were completely blocked on this feature because it goes against a major assumption of all major operating systems - that one user will use the system at a time. For instance, it’s assumed that one window will have focus at a time. It is also assumed that there is only one mouse cursor at a time. There are even more subtle issues that come up, such as there being only one copy and paste buffer.

At the time, I was disappointed, but I didn’t dwell on it too much. I figured that that level of multi-user collaboration would only service a very small niche market, and that it wasn’t so bad that we didn’t have the possibility of better collaboration.

Fast forward to today. I pair program most of the time, and I find myself longing for an environment that would allow me and my pair to collaborate in different ways. It’s standard pair programming, where we both work on the same window, and have to share the same keyboard and mouse input (preferably with 2 mice and 2 keyboards). So what do you do when one person has to check email, or one person decides to research an issue online, or otherwise engage in a task that is not the same as the other pair member? Typically, each person will have a laptop that they can use for these situations, but that causes the person to separate themselves from the primary work environment. Sharing information will have to done by sending an email or putting a file on a shared drive, or having the other person look at information on the laptop’s screen. What about spiking a problem side by side, where each pair tries two similar but different approaches to solving a programming problem? I would prefer to collaborate with someone in the same environment, where we can do some different tasks in parallel, and can share windows, applications, and data in the same desktop environment.

I think that it would be interesting to expand this idea even further. What if an entire team worked in the same environment, on the same desktop? Each person could have a portion of a huge desktop, and people in a team could choose to collaborate with each other in a very fluid fashion by putting their desktop spaces close together. It would be an interesting experiment in collaboration applied to software development.

Even if multiple simultaneous mouse and keyboard input were generally possible in operating systems, there would still likely be a lot of complex concurrency issues for developers, such as making sure that servers bind to different ports and write out to different files. It’s likely that once popular operating systems generally allow the multi-user features I mentioned before, that it would still be preferable to have isolated machines that interact through collaborative software rather then have everyone actually use the same machine.

I’ve seen software that allows sharing of a the mouse and keyboard across machines, and sharing the copy and paste buffer, but the purpose was to allows one person to connect 2 machines and use them like an extended desktop. Perhaps software like this could be tailored for better collaboration for pair programming or developers on a team not pairing.

An idea for a ruby inspection tool

October 25th, 2008

Working on some ruby code for a little while, I had an idea for a tool that would be useful.

Basically, what I want is an inspection tool that also has a bundle in textmate, so that I can see the history of objects that are created, as well as interactions in a form that is searchable and that quickly shows what really happened. It would be like having debugging on all the time.

The goal of this tool would be to allow developers to view what really happens with the code when it executes. Sometimes finding the source of bugs is difficult because statically analyzing code is not the easiest way to predict the future state of an object, or to predict the interactions between objects. You don’t really know what happens until the code executes.

Nor is traditional debugging an efficient way to get the information that you need. You have to set a break-point, add in a line to call the a debugger, or even print out some information in order to answer a question about the actual execution of the code.

What I want is a tool that records a wealth of data about the actual execution of some code, such as when I run a test, or initiate an action from a controller. Then, I would like to be able to filter/query that information to get more precise information.

For the inspection tool, I’d like to know things like:

- See the actual methods and fields of a class or object instance at the beginning and end of code execution
- See all calls made to an object, or a specific instance of an object
- See all callers of a specific method of a specific object type or object instance
- Detect changes in the structure of an individual object that happens, such as methods being added or redefined after the object is initialized
- View all changes to fields of an object
- I would like to see the order that objects are created in. I would like to see when all objects of a certain type were created, or objects that respond to a specific method
- I would like to see all interactions between 2 objects
- I would like to be able to further scope the search, so that I only see information within the scope of a specific method call

Preferably, I would want to be able to view all of this information after running the code one time. And then be able to enter some search criteria into a form and have it reduce the amount of information to match my search criteria. Then, I can tweak some starting data in a test, and then rerun the test and get a new set of information to query.

I think that most of this information could be obtained by creating a Module to intercept all method sends to every object, and save some data about the context of the call when it’s made. Then, a client tool could parse the output, and allow a developer to query it.

I tried to do some googling to find something like this, but all I found were standard debugging tools, which I don’t think have the features that I’m looking for. If anyone knows of something that can do what I want, let me know.

Granularity of Abstractions

October 23rd, 2008

In Java, iterating over a collection and collecting some objects based on a condition is a classic example of something to put into a method. Here is an example:

  1. private Collection<Claim> getExpiredClaims() {
  2.     List<Claim> expiredClaims = new ArrayList<Claim>();
  3.     for (Claim claim : allClaims) {
  4.         if (claim.isExpired()) {
  5.             expiredClaims.add(claim);
  6.         }
  7.     }
  8.     return expiredClaims;
  9. }

In Ruby, you can do this in one line.

  1. all_claims.select { |claim| claim.expired? }

It’s lovely how much more succinct that operation becomes. It can actually get more succinct by using Symbol#to_proc:

  1. all_claims.select (&:expired?)

There is still the question of whether to move this to a method. Now, moving it to a method doesn’t reduce multi-line duplication the way it would in the Java example; there is only a minor amount of single line duplication that can be reduced.

The resistance I often get to refactoring a single line to a method is that it creates more total lines in the file with the def/end lines and spacing. Also, it takes a slight amount of effort to do the refactoring, and some developers won’t refactor unless the need is glaring and obvious.

There are still important reasons to move this to a method, and those reasons are better readability of code and reducing duplication of expressions. Without the refactoring, you’ll see code that looks like this:

  1. def mark_expired_claims_for_review
  2.     all_claims.select (&:expired?).each(&:needs_review!)
  3. end
  4.  
  5. def notify_claim_agents_of_expired_claims
  6.     all_claims.select (&:expired?).each(&:notify_agent_of_expiration)
  7. end

After the refactoring, this is what it would look like.

  1. def expired_claims
  2.     all_claims.select(&:expired?)
  3. end
  4.  
  5. def mark_expired_claims_for_review
  6.     expired_claims.each(&:needs_review!)
  7. end
  8.  
  9. def notify_claim_agents_of_expired_claims
  10.     expired_claims.each(&:notify_agent_of_expiration)
  11. end

It’s a minor point perhaps, and I’m sure that I could come up with much more excessive examples, but I just wanted to focus on a simple example. By replacing the expression with a symbolic reference, a method in this case, you express in English something that is a programmatic operation. This improves readability quite a bit.

Also, there is the additional benefit of abstracting on the concept of expired claims. By having multiple places using “all_claims.select(&:expired?)” to express expired claims, you duplicate the implementation detail that expired claims are derived from a larger collection of claims. This may not always be true, and a change in that derivation results in a change in many places.

Perhaps the question here is how DRY should you make your code. I’m still undecided on this on this point, but I think that the amount of work that it takes to reduce duplication to this level is minimal, and the result will be an important part of a pristine codebase.

Getting rid of switch statements with Java Enums

October 19th, 2008

I recently saw an interesting and polymorphic way to get rid of using a case statement when using enums. This is possible by defining a method for each instance of an enum.

I’m sure that you have seen code like this:

  1. enum Friend {
  2.     Joey, Chandler;
  3. }

And then somewhere in the code, you might see:

  1. class SomeObjectThatNeedsToKnowBestFriends {
  2.  
  3.     void doSomethingWithBestFriends() {
  4.         for (Friend friend : Friend.values()) {
  5.             doSomething(bestFriend(friend));
  6.         }
  7.     }
  8.  
  9.     Friend bestFriend(Friend friend) {
  10.          switch (friend) {
  11.              case Joey: return Friend.Chandler;
  12.              case Chandler: return Friend.Joey;
  13.              default: throw new RuntimeException(“This person has no friend”);
  14.          }
  15.     }
  16. }

This is a common smell in a code base where client code has logic that should be better encapsulated. Now, whenever someone adds a friend, they are going to have to search for references on Friends and add a new entry, or they are going to get a RuntimeException from the switch statement. In a well tested codebase, there is probably going to be a unit test that asserts that all Friends have best friends. In any case, the switch statement in the client object code is not great from an OO standpoint and it creates a maintainability issue.

The first refactoring is to move all Friend related logic to the Friend enum where it belongs.

  1. enum Friend {
  2.     Joey, Chandler;
  3.  
  4.     Friend bestFriend() {
  5.          switch (this) {
  6.              case Joey: return Chandler;
  7.              case Chandler: return Joey;
  8.              default: throw new RuntimeException(“This person has no friend”);
  9.          }
  10.     }
  11.  
  12. }

Now, we can just ask the Friend who their best friend is.

  1. Joey.bestFriend(); –> returns Chandler;

Nice. Still, I’m not wild about that switch statement. Developers still have to know to update it, and really, I’d rather not even have to throw an exception because it was misused. It would be better if the structure of the code did not allow misuse.

Here is an example of how to do this:

  1. enum Friend {
  2.     Joey {
  3.         Friend bestFriend() { return Chandler; }
  4.     },
  5.     Chandler {
  6.         Friend bestFriend() { return Joey; }
  7.     }
  8.     abstract Friend bestFriend();
  9. }

Then,

  1. Joey.bestFriend(); –> returns Chandler;

Great, now we know that when someone adds a new friend, they will immediately be confronted with having to supply a best friend. My only issue with this approach is that all the method definitions become verbose when you introduce many methods like this. I tried different approaches to solving this problem, but due to the enums referencing each other, I was not able to do a different approach.

Here is an example of what you can’t do:

  1. enum Friend {
  2.     Joey (Chandler),
  3.     Chandler(Joey)
  4.  
  5.     final Friend bestFriend;
  6.  
  7.     Friend(Friend bestFriend) {
  8.         this.bestFriend = bestFriend;
  9.     }
  10.  
  11.     Friend bestFriend() {
  12.         return bestFriend;
  13.     }
  14. }

This won’t work because you can’t reference Chandler in the enum definition for Joey. The Chandler Enum hasn’t been defined yet, so this won’t even compile. However, you can “trick” the compiler by fully referencing Chandler using Friend.Chandler;

  1. enum Friend {
  2.     Joey (Friend.Chandler),
  3.     Chandler(Joey)
  4.  
  5.     final Friend bestFriend;
  6.  
  7.     Friend(Friend bestFriend) {
  8.         this.bestFriend = bestFriend;
  9.     }
  10.  
  11.     Friend bestFriend() {
  12.         return bestFriend;
  13.     }
  14. }

However, the result is not what we want:

Joey.bestFriend(); –> null
Chandler.bestFriend(); –> Joey

Even though I can reference the other enum instance this way, it resolves to null. The reason lies in the fact that when the Enum is compiled, each instance is a static final field, and initialized in a static block. Here is a snippet of the generated code:

  1. public static final Friend Joey;
  2.     public static final Friend Chandler;
  3.     final Friend bestFriend;
  4.     private static final Friend ENUM$VALUES[];
  5.  
  6.     static
  7.     {
  8.         Joey = new Friend(“Joey”, 0, Chandler);
  9.         Chandler = new Friend(“Chandler”, 1, Joey);
  10.         ENUM$VALUES = (new Friend[] {
  11.             Joey, Chandler
  12.         });
  13.     }

Interestingly, I can write a program that tries the fully qualified name for explicit static constants, and it works:

  1. public class StaticEnumClass {
  2.  
  3.     static final String foobar = “foo” + StaticEnumClass.bar;
  4.     static final String bar = “bar”;
  5.    
  6.     public static void main(String[] args) {
  7.         System.out.println(foobar); // prints out "foobar"
  8.     }
  9.    
  10. }

But if I use a static initialization, it doesn’t:

  1. public class StaticEnumClass {
  2.  
  3.     static final String foobar;
  4.     static final String bar;
  5.    
  6.     static {
  7.         foobar = “foo” + StaticEnumClass.bar;
  8.         bar = “bar”;
  9.     }
  10.    
  11.     public static void main(String[] args) {
  12.         System.out.println(foobar); // prints "foonull"
  13.     }
  14.    
  15. }

Interesting. The compiler is smart enough to resolve the correct value when you don’t use a static initialization block. Back to my original example with Friends. The next attempt was to create an anonymous constructor (actually, an instance initializer) for the enum instances and see if I could get what I want:

  1. enum Friend {
  2.     Joey {
  3.         {
  4.             bestFriend = Chandler;
  5.         }
  6.     },
  7.     Chandler {
  8.         {
  9.             bestFriend = Joey;
  10.         }
  11.     }
  12.  
  13.     Friend bestFriend;
  14.  
  15.     Friend bestFriend() {
  16.         return bestFriend;
  17.     }
  18. }

I had to remove the final from bestFriend, since I’m inializing the value when the object is instantiated using an instance initializer. This compiles, and seems like an okay approach. My hope was that the references to other enum types would get resolved in much the same manner as in the case of creating a method that returns each one. Interestingly, this doesn’t happen.

  1. Joey.bestFriend(); –> null
  2. Chandler.bestFriend(); –> Joey

The reason is that even though I am using an instance initializer, it’s being called from a static block since the instances are created in a static block. Turns out to be a naive attempt. Here is what it ends up looking like when the enum gets generated as a class:

  1. static
  2.     {
  3.         Joey = new Friend(“Joey”, 0) {
  4.  
  5.            
  6.             {
  7.                 bestFriend = Friend.Chandler;
  8.             }
  9.         }
  10. ;
  11.         Chandler = new Friend(“Chandler”, 1) {
  12.  
  13.            
  14.             {
  15.                 bestFriend = Friend.Joey;
  16.             }
  17.         }
  18. ;
  19.         ENUM$VALUES = (new Friend[] {
  20.             Joey, Chandler
  21.         });
  22.     }

Oh well. That’s as far as my experimentation went. I’m satisfied that I can at least create an anonymous subtype of an enum that returns the correct value, but if anyone has any ideas on how to do this in a cleaner way, let me know.