为什么java.awt.Color对每个Color都有两个变量?

Hun*_*Dev 1 java variables awt colors

摘录自java.awt.Color:

...
/**
 * The color white.  In the default sRGB space.
 */
public final static Color white     = new Color(255, 255, 255);

/**
 * The color white.  In the default sRGB space.
 * @since 1.4
 */
public final static Color WHITE = white; // My comment: THE SAME!!??
...
Run Code Online (Sandbox Code Playgroud)

从上面的摘录中可以看出,它Color white被分配给两个变量Color#WHITE,Color#white这也是同样的情况:

 - black     (and BLACK)
 - blue      (and BLUE)
 - cyan      (and CYAN)
 - darkGray  (and DARK_GRAY)
 - gray      (and GRAY)
 - green     (and GREEN)
 - lightGray (and LIGHT_GRAY)
 - magenta   (and MAGENTA)
 - orange    (and ORANGE)
 - pink      (and PINK)
 - red       (and RED)
 ->white     (and WHITE)<-discussed
 - yellow    (and YELLOW)
Run Code Online (Sandbox Code Playgroud)

最初,我曾经认为每种颜色都有两个名称是有原因的.但是当我检查源代码时,我才知道它们都具有相同的价值!


我想知道为什么每种颜色都有两个变量?

这种用法是否有任何具体原因(无论是历史性的,实际的等)?

最后,在我们的应用程序中使用哪两个?:

// THIS?:
Color newC = Color.white;

// OR THIS?:
Color newC = Color.WHITE;
Run Code Online (Sandbox Code Playgroud)

use*_*432 6

变量根据Java约定进行了更改,其中常量仅为大写.小写仍然存在兼容性并具有相同的值.

要遵循Java约定,您应该始终使用大写常量,如:

Color.RED
Run Code Online (Sandbox Code Playgroud)

  • 我不认为他们会很快被删除,因为他们会被标记为先弃用. (2认同)