views:

377

answers:

4

I just had an interview where one of the questions was something like "Describe 5 ways to use the static keyword in Java." I could only think of 2 on the spot, and afterwards I found 2 more. What is the 5th?

  1. Declaring a field belonging to a class as opposed to an instance of the class.
  2. Declaring a method that can be called on a class as opposed to an instance.
  3. Declaring a nested class as static
  4. Defining a static class initializer.
  5. ???
A: 

Constants - static final (which is really the same as #1, but could be consider a separate usage)

Ryan Emerle
Isn't that the same as "a field belonging to the class"?
Paul Tomblin
They are the same, but maybe the interviewer considers them different when used in an interface?
Hosam Aly
+13  A: 

static import (since java 1.5):

import static my.package.MyClass.*;

ChssPly76
This is the one you're looking for.
Dave Ray
Thank you! I saw this the day before, but I totally forgot about it.
Sam
+1  A: 

Would declaring a static interface be considered a class in this instance? If not then there's another use.

non sequitor
I think it's "Interfaces can do work" per Strange.java by Robert Sedgewick
Nicholas Jordan
A: 

create a static block

static 
{

 // Do some static work 

}
same as #4 - Defining a static class initializer "block"
non sequitor