java中'static int'和'int'之间的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2198848/
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 04:38:41  来源:igfitidea点击:

difference between 'static int' and 'int' in java

java

提问by vini

in java where and when do we use 'static int' and how does it differ from 'int'

在java中我们何时何地使用'static int'以及它与'int'有什么不同

回答by fastcodejava

staticmeans it is not instance specific. It belongs to the class. Usually it goes with final.

static意味着它不是特定于实例的。它属于class. 通常它与final一起使用。

public static final int MAX = 10000;  // Defined in MyClass

// Somewhere else you could do
int max = MyClass.MAX;  // notice no instance of MyClass needed.

EDIT : It does not have to be final, non final variables are fine as long as one is careful.

编辑:它不一定是最终的,只要小心,非最终变量就可以了。

回答by Rubens Farias

Take a look here: Understanding Instance and Class Members

看看这里:了解实例和类成员

When a number of objects are created from the same class blueprint, they each have their own distinct copies of instance variables. (...)

Sometimes, you want to have variables that are common to all objects. This is accomplished with the static modifier. Fields that have the static modifier in their declaration are called static fields or class variables.

当从同一个类蓝图创建多个对象时,它们每个都有自己不同的instance variables. (……)

有时,您希望拥有所有对象共有的变量。这是通过静态修饰符完成的。在其声明中具有 static 修饰符的字段称为静态字段或class variables.

回答by Noon Silk

Well.

好。

It's used when declare member variables, and staticrefers to whether or not you need an instanceof the given class to access it.

它在声明成员变量时使用,并static指您是否需要给定类的实例来访问它。

Typically, staticis used for constants, or "helper" functions or variables. If you have too many, and if you end up combining static and non-static variables in a class, it ends up being suggestive of bad design (not all the time, though).

通常,static用于常量,或“辅助”函数或变量。如果你有太多,如果你最终在一个类中组合静态和非静态变量,它最终会暗示糟糕的设计(但并非总是如此)。

If a variable is static, it's value is shared between all usages (i.e. between all instances of the object). That is, if you change it, all other accesses will see the change.

如果一个变量是static,它的值在所有用法之间共享(即在对象的所有实例之间)。也就是说,如果您更改它,所有其他访问都会看到更改。

A non-static variable will have a unique value per instance (as it can only be accessed perinstance).

非静态变量每个实例都有一个唯一值(因为它只能被每个实例访问)。

回答by Notinlist

static int: One variable per application Can be accessed without object.

static int:每个应用程序一个变量可以在没有对象的情况下访问。

int: One variable per object Cannot be accessed without object.

int:每个对象一个变量,没有对象就不能访问。

回答by soren.enemaerke

Using 'int' in a class means an integer field exists on each instance of the class. Using 'static int' means an integer field exists on the class (and not on each instance of the class)

在类中使用 'int' 意味着该类的每个实例上都存在一个整数字段。使用 'static int' 意味着类中存在一个整数字段(而不是类的每个实例)

回答by Modery

The modifier staticdefines a variable as a class variable, meaning that there is exactly one of it only. Without it, a variable is an instantiable variable, so this variable exist per Object.

修饰符static将变量定义为类变量,这意味着它只有一个。没有它,一个变量就是一个可实例化的变量,所以这个变量存在于每个对象中。

For example:

例如:

class Test {
  static int i;
  int j;
}

class Test 2 {
   public static void main(String args[])  {
     Test test1 = new Test();
     Test test2 = new Test();
     test1.i = 1;
     test1.j = 2;
     test2.i = 3;
     test2.j = 4;
     System.out.println("test1.i: "+test1.i);
     System.out.println("test1.j: "+test1.j);
     System.out.println("test2.i: "+test2.i);
     System.out.println("test2.j: "+test2.j);
   }
}

If you create 2 objects of the Testclass, both objects will "share" the ivariable. But each object will have its own jvariable.

如果创建Test类的2 个对象,则两个对象都将“共享” i变量。但是每个对象都有自己的j变量。

In the example above, the output will be

在上面的例子中,输出将是

test1.i: 3
test1.j: 2
test2.i: 3
test2.j: 4

You can read more about it at The Java Tutorials - Variables

您可以在The Java Tutorials - Variables 中阅读有关它的更多信息

回答by Satish

static int, which can be accessed directly without using objects. int, which cannot be accessed directly without using objects.

static int, 无需使用对象即可直接访问。 int,不使用对象就不能直接访问。