static
메서드에서 variable
에 액세스하려고합니다. 예를 들어정적 메서드에서 변수에 액세스하는 방법
:
그래서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
, 어떻게 정적 메서드에서 variable
의 value
에 액세스 할 수 있습니다 그래서 여기
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());
}
}
는 오류 코드입니까?
편집 :
내가 코드를 편집 한, 나는 단지 static
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();
}
}
내 프로그램이 Highest.txt
라는 이름의 파일에 액세스하는 데 사용됩니다
OK, 여기 내 프로그램 코드입니다. 하지만 내 메인 프로그램에 구현하려면 names
, hours
, minutes
, seconds
및 clicks
값을 가져와야합니다. 내 메인 프로그램에 구현하려고 할 때이 문제점을 발견했습니다.
내가이 코드를 위해 main
메서드를 만들면 별도로 처리하면 잘 작동합니다. 하지만 지금은 다른 작업을 수행하기 위해 메인 프로그램에서이 값을 가져와야합니다.
당신의'ABC()'메소드는 "정적 생성자"에 대한 시도처럼 의심 스럽습니다. 실제로는별로 의미가 없습니다. –
정적 변수가 아닌 함수 범위 변수에 액세스하려고합니다. – Nick
먼저 읽어보십시오. http://docs.oracle.com/javase/tutorial/java/index.html –