java中多个对象的修饰符

发布于 2024-11-14 20:20:04 字数 183 浏览 3 评论 0 原文

在java中可以做这样的事情吗?

static public final {
    String A = "a...";
    int B = 3;
    boolean C = true;
}

谢谢!

编辑:抱歉,我在示例中犯了一个错误..我不需要只字符串..

it is possible to do something like this in java?

static public final {
    String A = "a...";
    int B = 3;
    boolean C = true;
}

thanks!

EDIT: sorry i made a mistake in my example.. I don't need only Strings..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

无戏配角 2024-11-21 20:20:04

语法错误。但你可以这样做:

public static final String A = "a...", B = "b...", C = "c...";

或者,更接近你的版本(Java 中的空格无关紧要):

public static final String
   A = "a...",
   B = "b...",
   C = "c...";

作为参考,这里是 来自 href="http://java.sun.com/docs/books/jls/third_edition/html/j3TOC.html" rel="nofollow">Java 语言规范

FieldDeclaration:
    FieldModifiersopt Type VariableDeclarators ; # one variable type only

VariableDeclarators:
    VariableDeclarator
    VariableDeclarators , VariableDeclarator     # one or more variables

VariableDeclarator:
    VariableDeclaratorId
    VariableDeclaratorId = VariableInitializer

VariableDeclaratorId:
    Identifier
    VariableDeclaratorId [ ]

VariableInitializer:
    Expression
    ArrayInitializer

正如你所见(从我的评论中)您可以使用通用修饰符声明相同类型的多个字段,但不能混合类型。

Wrong Syntax. But you can do it like this:

public static final String A = "a...", B = "b...", C = "c...";

Or, closer to your version (white space is irrelevant in Java):

public static final String
   A = "a...",
   B = "b...",
   C = "c...";

For reference, here's the official grammar for Field Declarations from the Java Language Specification:

FieldDeclaration:
    FieldModifiersopt Type VariableDeclarators ; # one variable type only

VariableDeclarators:
    VariableDeclarator
    VariableDeclarators , VariableDeclarator     # one or more variables

VariableDeclarator:
    VariableDeclaratorId
    VariableDeclaratorId = VariableInitializer

VariableDeclaratorId:
    Identifier
    VariableDeclaratorId [ ]

VariableInitializer:
    Expression
    ArrayInitializer

So as you can see (from my comments) you can declare multiple fields of the same type with common modifiers, but you can't mix types.

南街九尾狐 2024-11-21 20:20:04

你的意思是像吗?

public interface Constants {
    String A = "a...";
    String B = "b...";
    String C = "c...";
}

在您的代码中,您可以使用静态导入。

import static Constants.*;

System.out.println(A);

Do you mean like?

public interface Constants {
    String A = "a...";
    String B = "b...";
    String C = "c...";
}

In your code you can use a static import.

import static Constants.*;

System.out.println(A);
梦醒灬来后我 2024-11-21 20:20:04

不,事实并非如此。
通常,首选 IDE 中的智能自动完成功能会有所帮助。
例如,在 netbeans 中,“Psfs”+ tab 扩展为 public static final String

No, it is not.
Usually, smart auto complete in a preferred IDE helps a bit.
E.g. in the netbeans, "Psfs" + tab expands to public static final String

脸赞 2024-11-21 20:20:04

不可以。目前没有 Java 版本允许您这样做。

即使它确实有用,我也承认。

No. At the moment no version of Java will permit you to do that.

Even if it would be really useful, I admit.

那小子欠揍 2024-11-21 20:20:04

您可以声明一个静态块,并在该静态块中创建变量:

public class TestS {
    static {
        String  a = "a...";
        int     b = 3;
        boolean c = true;
    }

但是一旦离开该静态块的范围,您将无法引用 ab ,或c

    public static void main(String[] args) {
        System.out.println(a); // won't compile
    }
}

在 main 中,a 无法解析为变量。

You may declare a static block and in that static block create variables:

public class TestS {
    static {
        String  a = "a...";
        int     b = 3;
        boolean c = true;
    }

But once you leave the scope of that static block, you wouldn't be able to reference a, b, or c.

    public static void main(String[] args) {
        System.out.println(a); // won't compile
    }
}

In main, a cannot be resolved to a variable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文