【初心者】Java質問・相談スレッド109【大歓迎】
■ このスレッドは過去ログ倉庫に格納されています
0246デフォルトの名無しさん
2007/11/29(木) 09:18:10staticの付いているフィールドはインスタンス化しないでも使えます。
定数にメソッドが付いているって感じです。以下、サンプル。
import java.io.*;
class MySystem
{
static int HOGEGE = 12345;
static PrintStream out;
static {
try {
out = new PrintStream
(new FileOutputStream("output.txt", true), true);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class HelloWorld
{
public static void main(String[] args)
{
for (int i = 0; i < 10; i++) {
MySystem.out.println(i + " " +
MySystem.HOGEGE + " " +
"Hello, world!");
}
}
}
■ このスレッドは過去ログ倉庫に格納されています