简体   繁体   中英

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

static import (since java 1.5):

import static my.package.MyClass.*;

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

要更改另一个静态方法/变量的行为。

常量 - 静态final(与#1真的相同,但可以考虑单独使用)

create a static block

static 
{

 // Do some static work 

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM