Sam
Sam

Reputation: 2630

5 ways to use the static keyword in Java

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. ???

Upvotes: 8

Views: 4249

Answers (5)

Rohit
Rohit

Reputation: 1194

To change the behaviour of another static method/variable.

Upvotes: 0

mmansoor
mmansoor

Reputation: 620

create a static block

static 
{

 // Do some static work 

}

Upvotes: -1

non sequitor
non sequitor

Reputation: 18806

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

Upvotes: 1

ChssPly76
ChssPly76

Reputation: 100776

static import (since java 1.5):

import static my.package.MyClass.*;

Upvotes: 16

Ryan Emerle
Ryan Emerle

Reputation: 15821

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

Upvotes: -1

Related Questions