java对象,共享变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2798485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 12:57:15  来源:igfitidea点击:

java objects, shared variables

javastatic-members

提问by raven

I have a simple question here. If I declare a variable inside an object which was made [declared] in the main class, like this:

我在这里有一个简单的问题。如果我在主类中 [declared] 的对象中声明一个变量,如下所示:

public static int number;

( usually I declare it like this :

(通常我这样声明

private int number;

)

)

can it be used in a different object which was also made [declared] in the main class? btw I do not care about security atm, I just want to make something work, don't care about protection)

它可以在主类中[声明]的不同对象中使用吗?顺便说一句,我不在乎安全性 atm,我只是想让一些东西工作,不在乎保护)

回答by Bozho

staticvariables are shared by all instances of a given class. If it's public, it is visible to everything. non-static variables belong to only one instance.

static变量由给定类的所有实例共享。如果是public,则所有内容都可见。非静态变量只属于一个实例。

Since your mainmethod is static, it can only see staticvariables. But you should avoid working statically - make an instance of a class, and pass the data around as method/constructor parameters, rather than sharing it via staticvariables.

由于您的main方法是static,因此它只能看到static变量。但是你应该避免静态工作——创建一个类的实例,并将数据作为方法/构造函数参数传递,而不是通过static变量共享它。

回答by polygenelubricants

Here's a telling quote from Java Language Specification:

这是 Java 语言规范中的一个有说服力的引用:

JLS 8.3.1.1 staticFields

JLS 8.3.1.1static字段

If a field is declared static, there exists exactly one incarnation of the field, no matter how many instances (possibly zero) of the class may eventually be created. A staticfield, sometimes called a class variable, is incarnated when the class is initialized.

A field that is not declared static(sometimes called a non-staticfield) is called an instance variable. Whenever a new instance of a class is created, a new variable associated with that instance is created for every instance variable declared in that class or any of its superclasses.

[Example program follows...]

如果声明static了一个字段,则无论最终创建多少个类的实例(可能为零),都只存在该字段的一个化身。一个static字段,有时称为类变量,在类初始化时体现。

未声明的字段static(有时称为非static字段)称为实例变量。每当创建类的新实例时,都会为该类或其任何超类中声明的每个实例变量创建与该实例关联的新变量。

[示例程序如下...]

In short, a staticfield is a classvariable: it belongs to the class, as opposed to the instancesof the class. In a way, you can think of a staticfield as a variable shared by instances of the class, but it's much more consistent to think of staticfields as belonging to the class, just like staticmethod also belongs to the class, etc.

简而言之,static字段是一个变量:它属于类,而不是类的实例。在某种程度上,您可以将 static字段视为由类的实例共享的变量,但将static字段视为属于类更一致,就像static方法也属于类等一样。

Since they belong to the class, they do not require an instance of said class to access (assuming adequate visibility), and in fact it's considered bad programming practice to access staticmembers through an instance instead of a type expression.

由于它们属于类,因此它们不需要访问所述类的实例(假设有足够的可见性),事实上,static通过实例而不是类型表达式访问成员被认为是糟糕的编程实践。

Related questions

相关问题

回答by extraneon

If the class holding 'number' is called MyClass you can refer to it as MyClass.number from any method.

如果持有“编号”的类称为 MyClass,您可以从任何方法将其称为 MyClass.number。

Doing so for a variable is not good design though.

但是,为变量这样做并不是一个好的设计。

回答by RMorrisey

There are really two issues here: public vs. private in the context of inner classes, and static variables.

这里确实有两个问题:内部类上下文中的公共与私有,以及静态变量。

Part 1:

第1部分:

staticmeans that you don't need an instanceof the class to access that variable. Suppose you have some code like:

static意味着您不需要类的实例来访问该变量。假设您有一些代码,例如:

class MyClass {
 public static String message = "Hello, World!";
}

You can access the property this way:

您可以通过以下方式访问该属性:

System.out.println(MyClass.message);

If you remove the statickeyword, you would instead do:

如果删除static关键字,则改为:

System.out.println(new MyClass().message);

You are accessing the property in the context of an instance, which is a copy of the class created by the newkeyword.

您在instance的上下文中访问该属性,它是由new关键字创建的类的副本。

Part 2:

第2部分:

If you define two classes in the same java file, one of them must be an inner class. An inner class can have a statickeyword, just like a property. If static, it can be used separately. If not-static, it can only be used in the context of a class instance.

如果在同一个 java 文件中定义了两个类,其中之一必须是内部类。内部类可以有一个static关键字,就像属性一样。如果是static,则可以单独使用。如果非静态,则只能在类实例的上下文中使用。

Ex:

前任:

class MyClass {
 public static class InnerClass {
 }
}

Then you can do:

然后你可以这样做:

new MyClass.InnerClass();

Without the 'static', you would need:

如果没有“静态”,您将需要:

new MyClass().new InnerClass(); //I think

If an inner class is static, it can only access staticproperties from the outer class. If the inner class is non-static, it can access any property. An inner class doesn't respect the rules of public, protected, or private. So the following is legal:

如果内部类是static,则它只能从外部类访问静态属性。如果内部类是非静态的,它可以访问任何属性。内部类不遵守publicprotectedprivate的规则。所以以下是合法的:

class MyClass {
 private String message;

 private class InnerClass {
  public InnerClass() {
   System.out.println(message);
  }
 }
}

If the inner class has keyword static, this would not work, since messageis not static.

如果内部类有关键字static,这将不起作用,因为消息不是静态的。