Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从静态方法访问变量_Java_Variables_Static - Fatal编程技术网

Java 如何从静态方法访问变量

Java 如何从静态方法访问变量,java,variables,static,Java,Variables,Static,我想从静态方法访问变量 例如: public class ABC { public static void ABC() { int abc = 123; int bcd = 234; } public int getabc() { int tempabc = abc; return tempabc; } public int getbcd() { in

我想从
静态
方法访问
变量

例如:

public class ABC
{
    public static void ABC()
    {
        int abc = 123;
        int bcd = 234;
    }
    public int getabc()
    {
        int tempabc = abc;
        return tempabc;
    }
    public int getbcd()
     {
        int tempbcd = bcd;
        return tempbcd;
    }
   public static void main(String[] args)
    {
        System.out.println(ABC.getabc());
    }
}
下面是错误代码:

error: cannot find symbol
        int tempabc = abc;
                      ^
symbol:   variable abc
location: class ABC

error: cannot find symbol
        int tempbcd = bcd;
                      ^
symbol:   variable bcd
location: class ABC

error: non-static method getabc() cannot be referenced from a static context
        System.out.println(ABC.getabc());
                              ^
3 errors
那么,如何从静态方法访问
变量

编辑:

我已经编辑了代码,我只想从
静态
abc()
中获取
abc
的值。但是根据上面的示例代码,编译时会显示错误

示例代码与程序代码的样式相同

好的,这是我的程序代码:

import java.io.*;
import java.util.*;

public class ReadHighestScoreFile
{   
    public static void ReadHighestScoreFile() throws IOException
    {
        final int NAME_SIZE = 35;
        String name = "";
        public static String names = 0;
        static int hours, minutes, seconds, clicks;

        File file = new File("Highest.txt");
        RandomAccessFile out = new RandomAccessFile(file, "rw");

        for (int i = 0; i < NAME_SIZE; i++)
        {
            name += out.readChar();
        }

        names = name;
        hours = out.readInt();
        minutes = out.readInt();
        seconds = out.readInt();
        clicks = out.readInt();

        System.out.println(">> Name : " + names);
        System.out.println(">> Hour : " + hours);
        System.out.println(">> Minute: " + minutes);
        System.out.println(">> Second : " + seconds);
        System.out.println(">> Click : " + clicks);     

        out.close();
    }
}
import java.io.*;
导入java.util.*;
公共类ReadHighestScoreFile
{   
public static void ReadHighestScoreFile()引发IOException
{
最终整数名_SIZE=35;
字符串名称=”;
公共静态字符串名称=0;
静态整数小时、分钟、秒、点击次数;
File File=新文件(“Highest.txt”);
RandomAccessFile out=新的RandomAccessFile(文件“rw”);
对于(int i=0;i>名称:”+名称);
System.out.println(“>>小时:“+小时”);
System.out.println(“>>分钟:+分钟);
System.out.println(“>>秒:+秒);
System.out.println(“>>单击:”+单击);
out.close();
}
}
我的程序用于访问名为
Highest.txt
的文件。但是我需要获取
名称
小时
分钟
、以及
单击
的值,以实现我的主程序。当我试图将它实现到我的主程序时,我发现了这个问题


如果我单独做,这意味着我为这段代码创建了一个
main
方法,它会工作得很好。但是现在我需要为我的主程序获取这些值以执行其他操作。

abc
是一个方法局部变量。无法在该方法之外访问它。

abc
是一个方法局部变量。无法在该方法之外访问它。

您需要在类范围内声明它:

public static int abc = 123;

您需要在类范围中声明它:

public static int abc = 123;

abc
对于
abc()
具有局部作用域。如果您想将其取回,可以从
ABC()
返回
int

public static int ABC()
{
    int abc = 123;
    return abc;
}

public static void main(String[] args)
{
    System.out.println(ABC());
}

abc
对于
abc()
具有局部作用域。如果您想将其取回,可以从
ABC()
返回
int

public static int ABC()
{
    int abc = 123;
    return abc;
}

public static void main(String[] args)
{
    System.out.println(ABC());
}

尝试添加返回字符串的get:

public static String getABC()
{
    string i = "abc";
    return i;
}
然后调用该函数:

System.out.println(getABC());

尝试添加返回字符串的get:

public static String getABC()
{
    string i = "abc";
    return i;
}
然后调用该函数:

System.out.println(getABC());

您的变量不是类变量,而是方法变量。若使用静态变量,请确保您知道自己在做什么。使用它们通常是个坏主意。 下面是访问静态变量的方法

public static class ABC {
  public static int someInt = 123;
}

public static class MainClass {

  public static void main(String[] args) {
    System.out.println(ABC.someInt);
  }
}

您的变量不是类变量,而是方法变量。若使用静态变量,请确保您知道自己在做什么。使用它们通常是个坏主意。 下面是访问静态变量的方法

public static class ABC {
  public static int someInt = 123;
}

public static class MainClass {

  public static void main(String[] args) {
    System.out.println(ABC.someInt);
  }
}

你真的试过编译代码吗?如果是,那么您可能没有注意到代码中存在很多问题

  • 在这里的“int tempabc=abc;”中,您试图在另一个方法中访问变量,该方法在声明它的方法之外是不可见的

  • 同样的上述事件在这里是“int tempbcd=bcd


  • 3.您的问题的答案是,您无法从静态方法访问非静态方法。将访问说明符static添加到需要从main访问的方法。

    您真的尝试过编译代码吗?如果是,那么您可能没有注意到代码中存在很多问题

  • 在这里的“int tempabc=abc;”中,您试图在另一个方法中访问变量,该方法在声明它的方法之外是不可见的

  • 同样的上述事件在这里是“int tempbcd=bcd


  • 3.您的问题的答案是,您无法从静态方法访问非静态方法。将访问说明符static添加到您需要从main访问的方法。

    您的帖子有冲突的语句:

    1. how to access variable from a static class?  
    
    您的代码没有定义静态类,而是定义了一个与类ABC同名的静态方法

    2. static method that has same name as class `ABC`.
    
    如果您认为这是创建静态类的方法,那么这是错误的。静态类只能是内部到外部类

    3. tempabc = abc.
    
    即使您定义了一个静态类,也不能以这种方式访问非静态变量。除非变量也是静态的,否则需要创建静态类的实例并以适当的方式访问变量值

    回答你帖子的第二部分
    您应该定义一个类来封装所述字段,定义如下:

    class HighestData {
        String names;  
        int hours;  
        int minutes;  
        int seconds;  
        int clicks;  
    
        // define setter, getter methods or a constructor with input params for above fields.
    }
    
    将方法从
    void
    更改为
    返回最高数据的对象

    public static HighestData ReadHighestScoreFile() throws IOException {  
        // your code here  
        // ...  
        HighestData data = new HighestData();  
        data.setName( name );  
        data.setHours( out.readInt() );  
        data.setMinutes( out.readInt() );  
        data.setSeconds( out.readInt() );  
        data.setClicks( out.readInt() );  
        // ...  
        return data;  
    }  
    
    ReadHighestScoreFile()
    方法的调用方现在可以处理
    HighestData
    对象,并使用它进行进一步处理


    最后但很重要的一点是,与C不同,您不能在Java中定义本地静态字段。他们必须达到班级水平

    您的帖子有相互冲突的声明:

    1. how to access variable from a static class?  
    
    您的代码没有定义静态类,而是定义了一个与类ABC同名的静态方法

    2. static method that has same name as class `ABC`.
    
    如果您认为这是创建静态类的方法,那么这是错误的。静态类只能是内部到外部类

    3. tempabc = abc.
    
    即使您定义了一个静态类,也不能以这种方式访问非静态变量。除非变量也是静态的,否则需要创建静态类的实例并以适当的方式访问变量值

    回答你帖子的第二部分