I want to access a variable from a static method.
For example :
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());
}
}
So here's the error code :
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
So, how can I access the value of the variable from a static method?
EDIT :
I have edited the code, and I just want to get the value of abc from the static ABC(). But based on the sample code above, it's show error when compiled.
The sample code is have the same style of the program code.
OK, here is my program code :
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();
}
}
My program is used to access a file named Highest.txt. But I need to get the values of names, hours, minutes, seconds, and clicks to implement to my main program. I found this problem when I tried to implement it to my main program.
If I doing it seperately, meaning that I create a main method for this code, it will work fine. But now I need to get these values for my main program to perform other operations.
ABC()method looks suspiciously like an attempt at a "static constructor", which doesn't really make much sense.