我可以在Java中的静态成员函数中声明一个静态变量吗? [英] Can i declare a static variable inside static member function in Java?

查看:151
本文介绍了我可以在Java中的静态成员函数中声明一个静态变量吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我得到一个错误Illegal Modifier我删除静态关键字没有错误,我需要这些变量是静态的

解决方案

你不能声明varibale静态方法。


内部方法所有变量都是局部变量,在这个方法之外不存在这就是为什么它们不能是静态的。

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

您正在尝试为斐波那契编写代码,并且您不需要静态变量,因为这里有一些链接描述了该解决方案。



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 /

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

解决方案

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.

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

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/

这篇关于我可以在Java中的静态成员函数中声明一个静态变量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆