8

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. ???
Charles
  • 50,943
  • 13
  • 104
  • 142
Sam
  • 2,620
  • 2
  • 26
  • 28
  • Are you sure it wasn't "final"? I use that one. :) – Jonathan Feinberg Oct 17 '09 at 00:25
  • I'm thinking probably not final because I think you can use "final" independently from "static". – Sam Oct 17 '09 at 01:19
  • What a crap interview question. Your ability or otherwise to come up with all five quickly under interview circumstances doesn't really demonstrate Java knowledge IMO. Not being able to name them all on demand doesn't mean you would be unable to use them effectively in practise. – funkybro Oct 03 '12 at 12:07

5 Answers5

16

static import (since java 1.5):

import static my.package.MyClass.*;

ChssPly76
  • 99,456
  • 24
  • 206
  • 195
1

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

non sequitor
  • 18,296
  • 9
  • 45
  • 64
0

To change the behaviour of another static method/variable.

theblackpearl
  • 1,164
  • 3
  • 15
  • 34
-1

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

Ryan Emerle
  • 15,461
  • 8
  • 52
  • 69
-1

create a static block

static 
{

 // Do some static work 

}
mmansoor
  • 580
  • 5
  • 11