在Java中定义常量字符串? [英] Defining constant string in Java?

查看:37
本文介绍了在Java中定义常量字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个常量字符串列表,需要在Java程序中的不同时间显示.

I have a list of constant strings that I need to display at different times during my Java program.

在C语言中,我可以在代码顶部定义这样的字符串:

In C I could define the strings like this at the top of my code:

#define WELCOME_MESSAGE "Hello, welcome to the server"
#define WAIT_MESSAGE "Please wait 5 seconds"
#define EXIT_MESSAGE "Bye!"

我想知道在Java中做这种事情的标准方法是什么?

I am wondering what is the standard way of doing this kind of thing in Java?

推荐答案

通常,您需要在类顶部定义它:

Typically you'd define this toward the top of a class:

public static final String WELCOME_MESSAGE = "Hello, welcome to the server";

当然,请根据使用此常量的位置使用适当的成员可见性( public / private / protected ).

Of course, use the appropriate member visibility (public/private/protected) based on where you use this constant.

这篇关于在Java中定义常量字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆