---
title: "JAVA声明静态常量"
author: "robinrain"
date: "2014-08-29T15:38:40Z"
tags: []
categories: ["编程语言"]
slug: "java"
draft: false
lastmod: "2014-08-29T15:38:40Z"
id: 2110796
url: "https://robinrain.iteye.com/blog/2110796"
---

参考自：[http://dengminhui.iteye.com/blog/483530](http://dengminhui.iteye.com/blog/483530) 

参考自：http://chenxiuheng.iteye.com/blog/996385

 

常用的声明方式（使用静态代码块）：

Java代码  [![收藏代码](http://chenxiuheng.iteye.com/images/icon_star.png)]()

1. public final static Map map = new HashMap();
2. static {
3. map.put("key1", "value1");
4. map.put("key2", "value2");
5. }

一种简约的方式：

Java代码  [![收藏代码](http://chenxiuheng.iteye.com/images/icon_star.png)]()

1. public final static Map map = new HashMap() {{
2. put("key1", "value1");
3. put("key2", "value2");
4. }};

这种方式，相当于重载HashMap的一个匿名实现。向原有的HashMap中添加了一个匿名构造方法。 

另外附加一个List/Set变量的声明方式：

Java代码  [![收藏代码](http://chenxiuheng.iteye.com/images/icon_star.png)]()

1. public final static List list = Arrays.asList("elment1", "element2");