java 静态方法中的局部变量也是静态的吗?

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

Are local variables in static methods also static?

javamemory-managementstaticstatic-methods

提问by peter

I am wondering do all the local variables become static if we declare them in a static method ?

我想知道如果我们在静态方法中声明它们,所有局部变量都会变成静态吗?

for example:

例如:

  public static void A(){
        int x [] = {3,2};
        changeX(x);

        for (int i = 0; i< x.length; i++){
             System.out.println(x[i]);   // this will print -1 and 1
        }
  }
  private static void changeX(int[] x){
        x[0] = -1;
        x[1] =  1;
  }

As far as I understand that Java is pass by value always, but why the state of X has changed after we made the changeX call ? Can anyone explain that please ? and can anyone explains how does Java deal with static variables in terms of memory allocation ? and what happen if we pass a static variable to a function as a parameter (I know people won't normally do that)

据我所知,Java 总是按值传递,但是为什么在我们进行 changeX 调用后 X 的状态发生了变化?谁能解释一下?谁能解释 Java 如何处理内存分配方面的静态变量?如果我们将静态变量作为参数传递给函数会发生什么(我知道人们通常不会这样做)

回答by Ernest Friedman-Hill

The answer to most of your questions is "the same as any other variable."

大多数问题的答案是“与任何其他变量相同”。

Local variables in static methods are just local variables in a static method. They're not static, and they're not special in any way.

静态方法中的局部变量只是静态方法中的局部变量。它们不是静态的,也没有任何特别之处。

Static variablesare held in memory attached to the corresponding Classobjects; any objects referenced by static reference variables just live in the regular heap.

静态变量保存在附属于相应Class对象的内存中;静态引用变量引用的任何对象都位于常规堆中。

When you pass a static variable to a method as an argument... absolutely nothing interesting happens.

当您将静态变量作为参数传递给方法时……绝对没有任何有趣的事情发生。

Regarding the scenario in your code:

关于您的代码中的场景:

  1. Imagine that you have a toy balloon on a string (the balloon is your array object, and the string is the reference to it declared in A().)
  2. Now you tie anotherstring on to the balloon and hand that string to a friend (that's exactly what happens when you call the changeX()method: the string is the parameter of the method, and it points to the same object.)
  3. Next, your friend pulls in the string, takes a black marker and draws a face on the balloon (this is like the changeX()method modifying the array).
  4. Then your friend unties his string, leaving just your string attached to the balloon (the method returns, and the local variable in changeX()goes out of scope.)
  5. Finally you reel in the string and look at the balloon: of course, you see the face (your A()routine sees the changed array.)
  1. 想象一下,您在一个字符串上有一个玩具气球(气球是您的数组对象,而该字符串是在A(). 中声明的对它的引用)
  2. 现在您将另一个字符串系在气球上并将该字符串交给朋友(这正是您调用该changeX()方法时发生的情况:该字符串是该方法的参数,它指向同一个对象。)
  3. 接下来,你的朋友拉入绳子,拿一个黑色记号笔在气球上画一张脸(这就像changeX()修改数组的方法)。
  4. 然后你的朋友解开他的字符串,只留下你的字符串附加到气球上(方法返回,局部变量 inchangeX()超出范围。)
  5. 最后,您收起绳子并查看气球:当然,您会看到脸部(您的A()例程会看到更改后的数组。)

It's really as simple as that!

真的就是这么简单!

回答by Julie in Austin

As others have pointed out, the variables which are local to a METHOD are the same as any other variable declared within any other method -- they are dynamically allocated and may be freed when the method returns the the variable is no longer visible.

正如其他人指出的那样,方法的局部变量与在任何其他方法中声明的任何其他变量相同——它们是动态分配的,并且可以在方法返回变量不再可见时被释放。

However, if you need static variables, you would have to declare them outside the methods, as ordinary static variables for a class. If, by convention, you leave them alone except when inside that particular method, they have the same effect as if they were static and local to the method. Just be sure to add comments to that effect.

但是,如果您需要静态变量,则必须在方法之外将它们声明为类的普通静态变量。如果按照惯例,除非在该特定方法中,否则您将它们单独放置,则它们的效果与它们是静态的和局部于方法的效果相同。请务必为此添加注释。

回答by Don Li

Static variables are stored in a special area of the heap called the "permanent generation".

静态变量存储在称为“永久代”的堆的特殊区域中。

Local variables delcared in a static method do not make any difference with those declared in a non-static method. Object references and primitive variables are placed on the stack.Whenever you create an object, the storage is allocated on the heap when that code is executed.

静态方法中声明的局部变量与非静态方法中声明的局部变量没有任何区别。对象引用和原始变量放在堆栈上。无论何时创建对象,执行该代码时都会在堆上分配存储空间。