« Eclipse: Gracefully Loading a Heavy Plugin | Main | Asking Knowledge Questions during Job Interviews »

Jul 23, 2008

Java Riddle: Static Members in Inner Classes?

Here's something that can annoy the most experienced Java developers and is guaranteed to make people to mumble in job interviews (if that's your thing).


Consider the following piece of code:


Java Riddle

On the face of it, the two variables are identical, but of a different type. So what's the problem here? 

Hint 1: This will work with a String. It will not work with an int array.

Hint 2:  This is the error you'll be getting (at least in Eclipse):

The field OBJECT_CONST cannot be declared static; static fields can only be declared in static or top level types

This hint is only more confusing. The error seems to apply to the integer field as well. So, what's the deal here?

If you know the answer, leave a comment. 

Updated: Check out the answer here.

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/services/trackback/6a00d83548421753ef00e553b2d3918833

Listed below are links to weblogs that reference Java Riddle: Static Members in Inner Classes?:

Comments

This is a tricky one :)

Looking at the Java spec, the exception seems to be for "Compile-Time Constant Expressions", which can be guaranteed at compile-time to "complete normally", i.e. they are guaranteed to never, ever throw an exception at runtime. Any other Java object may possibly throw an exception at runtime. This is even true for arrays of CTCEs because an array with a negative size will compile just fine but then blow up at runtime.

Still not sure why the restriction applies to inner classes specifically. I'll keep diggin'. :)

This would be a really stupid question to ask in an interview.

JLS sez Inner classes cannot have static declarations EXCEPT when the declared is a compile-time constant, such as a primitive or a literal String.

When you said "This will work with a String" you were slightly misleading - it will only work with a String literal, not with this:

private static final String boo = null;


Of course this sort of thing makes a terrible interview question. I think I'd walk out the door if an interviewer asked me this. (Well - I'd answer, then walk out the door)

Hint3:
private static final String ACONST = "";
will work -as you said- but
private static final String BCONST = null;
won't.

Its because the object is marked final. You can't declare a final object null. If you could it would be forever null, and what would the point of that be?

If you want to assign something to it later (just the once) then leave off the '= null' bit.

@Tzvika, you are correct. I hope you had to check the spec for that :-)

@Venkman, yes, this is a great hint. Very confusing.

And to everybody - I didn't seriously mean that you should use this question in a job interview.

some guys on job interviews ask questions just to feel superior, so it might be a good find for them. however sometimes it might be more, i was once asked about integer equals. i thought it was just checking if i read news on java from net.

however its the same with sun certification, most of the questions are stupid and you will never need it if you use IDE and not vi or dos edit(its faster).

I think its a bug, but you might this one day this is java certification exam.

yes, you can JadedDev. Try this code

public class Z {
public static final String a = null;


private class A {
public static final String a = null;
}
}

static is ambigue here. Are those fields static in reference to MyInner or to TestInnerStaticFinal? Consider:

t1 = new TestInnerStaticFinal();
t2 = new TestInnerStaticFinal();
t1.MyInner.OBJECT_CONST = something;
// what is the value of t2.MyInner.OBJECT_CONST now?

That's why "static fields can only be declared in static or top level types".

The strange case is, why the INT_CONST works. I assume that's because it's 'final', thus the compiler can insert the immediate value everywhere and solve the ambiguity. I haven't tried it, but the INT_CONST should also produce an error, if you remove the 'final' qualifier.

@JadedDev: Obviously you're wrong, but it's instructive to consider your "argument" and think about the different ways in which it's not a particularly good one (in addition to just being technically incorrect).

@Andreas,

A static variable has a value which is shared among all the instances of a given class. If you move the static variable to the top-level class, this will work. If you declare the inner class static (which makes it nested and not inner) it will work.

As the spec notes - only compile-time constant fields can be used.

its not such a bad interview question if the answer is "I would have to check the language specification. oh, here it is in section 8.1.3 and 15.28".

@dmh2000,

Having seen some responses here and over at DZone, I completely changed my mind. This is a great interview question. People need to keep in mind that the purpose of interview questions is not to get an answer. This is the purpose of tests (like the Sun certification). The purpose of interview questions is to observe how the candidate deals with the question: his/her approach, way of thinking, interaction and, of course, creativity and resourcefulness. I think it deserves a post on its' own.

It is an okay question, depending on how it is posed. I would instruct the interviewee that it's not the right answer that matters, but rather the process at getting the answer. dmh2000's approach would be a perfectly great answer: request the time to look at the spec and explain/arrive at the answer with the interviewer.

The idea is to recreate a possible problem in the work environment and investigate how the interviewee would solve it...

In IT, determining a candidate's ability in independent problem-solving is as important as determining experience and existing knowledge.

I wrote another post regarding the subject of using this question in an interview. You're welcome to read and comment.

http://zvikico.typepad.com/problog/2008/07/asking-knowledge-questions-during-job-interviews.html

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment

About nWire

  • nwire logo
    nWire is an Eclipse™ Plug-in which expedites Java development by assisting the developer in navigating through the code and better understanding it.

    Learn more at nwiresoftware.com
My Photo

My Other Accounts

Delicious Digg Facebook Flickr FriendFeed Google Talk Last.fm LinkedIn Reddit Skype StumbleUpon Technorati Yahoo!

AddThis Social Bookmark Button
Blog powered by TypePad
Member since 05/2007