我可以在 Java 的静态成员函数中声明一个静态变量吗?

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

Can i declare a static variable inside static member function in Java?

javafunctionstaticlocal-variables

提问by Raghu

private static int Fibonoci(int n) {
static int first=0;
static int second=1;
static int sum;
if(n>0)

i am getting a error "Illegal Modifier" and if i remove static keyword there is no error and i need those variables to be static

我收到一个错误“非法修饰符”,如果我删除 static 关键字没有错误,我需要这些变量是静态的

回答by Ashish Aggarwal

You can not declare varibale as static inside a method.
Inside method all variables are local variables that has no existance outside this method thats why they cann't be static.

您不能在方法中将 varibale 声明为静态。
在方法内部,所有变量都是局部变量,在此方法之外不存在,这就是为什么它们不能是静态的。

static int first=0;
static int second=1;
static int sum;
private static int Fibonoci(int n) {
   //do somthing
}

You are trying to write code for fibonacci series and for that you don't need static variables for that just here is some links who describes the sol for that

您正在尝试为斐波那契数列编写代码,为此您不需要静态变量,这里有一些链接,描述了为此的 sol

http://crunchify.com/write-java-program-to-print-fibonacci-series-upto-n-number/

http://crunchify.com/write-java-program-to-print-fibonacci-series-upto-n-number/

http://electrofriends.com/source-codes/software-programs/java/basic-programs/java-program-find-fibonacci-series-number/

http://electrofriends.com/source-codes/software-programs/java/basic-programs/java-program-find-fibonacci-series-number/

回答by Bathsheba

statics at function scope are disallowed in Java.

static在 Java 中不允许在函数范围内使用 s。

回答by Prashant K

The Root cause:Static Variables are allocated memory at class loading timebecause they are part of the class and not its object.

根本原因:静态变量在类加载时被分配内存,因为它们是类的一部分而不是它的对象。

Now, if static variable is inside a method, then that variable comes under the method's scope and JVM will be unable to allocate memory to it.

现在,如果静态变量在方法内,那么该变量在方法的范围内,JVM 将无法为其分配内存。

回答by Suresh Atta

Local variablescannot be declared static. In other words Staticdoesn't apply to local variables.

Local variables不能声明为静态。换句话说Static,不适用于local variables.

And I didn't see any use of declaringthem staticthere.

我在 那里没有看到declaring它们的任何用途static

Follow JLs on static fields

在静态字段上遵循JL

A static field, sometimes called a class variable, is incarnated when the class is initialized (§12.4).

静态字段,有时称为类变量,在类初始化时体现(第 12.4 节)。

回答by M. Abbas

You can't declare a static variable inside a method, staticmeans that it's a variable/method of a class, it belongs to the whole class but not to one of its certain objects. This means that statickeyword can be used only in a 'class scope' i.e. it doesn't have any sense inside methods.

您不能在方法中声明静态变量static,这意味着它是一个类的变量/方法,它属于整个类,但不属于其某些对象之一。这意味着static关键字只能在“类范围”中使用,即它在方法内部没有任何意义。

I don't know what you are trying to achieve, but if you really want these variables to be static then you can declare them as static fields in your class.

我不知道你想达到什么目的,但如果你真的希望这些变量是静态的,那么你可以在你的类中将它们声明为静态字段。

回答by gesus

You have to define static variables as members in class. Variables those are defined within method are local variables and their lifecycles ends at the end of the method. local variables are call specific, member variables are object specific and static variables are class specific variables.

您必须将静态变量定义为类中的成员。在方法中定义的变量是局部变量,它们的生命周期在方法结束时结束。局部变量是特定于调用的,成员变量是特定于对象的,静态变量是特定于类的变量。

回答by Dahaka

You need to declare the static variables outside of the function:

您需要在函数之外声明静态变量:

static int first=0;
static int second=1;
static int sum;
private static int Fibonoci(int n) {
    if(n>0)

回答by Manish Doshi

You can not declare varibale as static inside a method. In otherwords we can say that, Local variables cannot be declared static.

您不能在方法中将 varibale 声明为静态。换句话说,我们可以说,局部变量不能被声明为静态的。

回答by Azad

This varibles called Local Variables, they are inside method scop or constructor, they can't be instance or class variables.

这个变量称为局部变量,它们在方法范围或构造函数中,它们不能是实例或类变量。

private static int COUNT;// Class Variable
private static int Fibonoci(int n) {
 int a =3 ; // local variable
}

I need those variables to be static, okey , Why do you need this? because static variables used for special purpuse, however, you can create static fields like I did above code.

我需要这些变量是静态的,好吧,你为什么需要这个?因为静态变量用于特殊目的,但是,您可以像我上面的代码一样创建静态字段。