为什么我们使用 Static 关键字来初始化变量
我是一个绝对的初学者。我对这个打印斐波那契数列的递归程序有疑问。
我的问题是: 为什么他们在第二行代码中使用 static 关键字来声明整型变量 n1、n2 和 n3 ?
另外为什么他们在第三行代码中使用 static void 作为递归函数 printFibonacci(int count) ?
class FibonacciExample2{
static int n1=0,n2=1,n3=0;
static void printFibonacci(int count)
{
if(count>0){
n3 = n1 + n2;
n1 = n2;
n2 = n3;
System.out.print(" "+n3); printFibonacci(count-1);
}
}
public static void main(String args[]){
int count=10;
System.out.print(n1+" "+n2);//printing 0 and 1 printFibonacci (count-2);
}
}
I'm an absolute beginner. I have a question regarding this recursive program which prints Fibonacci series.
My question is:
why they used static keyword to declare the integer variable n1, n2 and n3 in the 2nd line of code ?
Also why they use static void for the recursive function printFibonacci(int count)
in the 3rd line of code?
class FibonacciExample2{
static int n1=0,n2=1,n3=0;
static void printFibonacci(int count)
{
if(count>0){
n3 = n1 + n2;
n1 = n2;
n2 = n3;
System.out.print(" "+n3); printFibonacci(count-1);
}
}
public static void main(String args[]){
int count=10;
System.out.print(n1+" "+n2);//printing 0 and 1 printFibonacci (count-2);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
静态成员存在于所有实例化对象中。您可以将静态成员视为某个类的所有对象共享的成员。修改静态成员时应该小心,因为该成员将在所有对象中被修改。静态方法是无需对象即可调用的方法。在你的情况下 FibonacciClass.method()。但是,在静态方法中只能使用静态成员,因为它们不依赖于对象。
Static members are present in all the instantiated objects. You can see static members as members shared by all objects of a certain class. You should be careful when modifying a static member as this member will be modified in all objects. Static methods are methods that can be called without an object. In your case FibonacciClass.method(). However in static methods only static members can be used as these are not dependent upon an object.
所以你不需要为它创建静态字段
}
就像这样
如果你提供 5 个全加,结果就会返回, 。检查方法如何在堆栈内存中工作,以了解这一点。
So you don't need to create static fields for it
}
it works like this if you provide 5
add all ones and the answer will be returned. check how methods are working in stack memory to Cleary understand this.
在这种情况下,我想他们已经使用了静态词,因为他们想直接在主方法中使用方法(printFibonacci)和字段(n1,n2,n3)(java要求它是静态的)。静态方法只能访问静态数据成员。
我想您已经知道有两种类型的成员 - 静态和动态(实例)。静态属于类本身,而动态属于该类的各个实例。因此,如果您想使用类的动态成员(字段、方法等),您将被迫创建此类的实例。
因此,如果他们将 n1、n2、n3 和 printFibonacci 方法声明为非静态,他们将被迫创建 FibonacciExample2 类的实例并通过该实例访问它们,这是完全毫无意义且冗长的。
In this exact case, I suppose they've used the static word since they want to use the method(printFibonacci) and the fields(n1, n2, n3) directly in the main method(which java requires to be static). Static methods can access only static data members.
I suppose you already know that there are two types of members- static and dynamic(instance). The static belong to the class itself, whereas the dynamic ones belong to individual instances of this class. Therefore, if you want to use a dynamic member(field, method, etc.) of a class, you would be forced to make an instance of this class.
So, if they had declared the n1,n2,n3 and the printFibonacci method to be non-static, they would be forced to make an instance of the FibonacciExample2 class and access them through that instance, which is utterly pointless and verbose.