そもそも、staticフィールドは該当クラスがロードされた際に
JVM上のヒープ領域内に1つ展開されるClassオブジェクト内に作成されます。
そのため、JVM起動から終了までstaticフィールドの箱は1つしかありません。
また、ヒープ内のClassオブジェクトとして展開されるため、インスタンス化しなくても利用することができます。
また、final修飾子は初期化時に設定した値を、そのJVM上では二度と変更しないためのもののため
これを外さなければ、値を更新することはできません。
なので、単純に考えるとご希望の動作は実現不可能です。
が、裏技を使えばできなくもなさそうです。
やり方は
1.Mainのhogeをcallして処理結果をreturnするクラスを用意する
2.testメソッドでそのクラスをコンパイルしたclassを実行する
3.処理結果を取得して検証する
という感じでいけるかもしれません。
以下のソースでそれっぽいものが行けました。
java
1 //標準入力を促すMainクラス
2 public class Main {
3 public static int hoge = get ( ) ;
4
5 private static int get ( ) {
6 try {
7 return System . in . read ( ) ;
8 } catch ( IOException e ) {
9 e . printStackTrace ( ) ;
10 }
11 return 0 ;
12 }
13 }
14
15 //そのMainクラスを実行するクラス
16 public class ExecMain {
17
18 public static void main ( String [ ] args ) {
19 System . out . println ( Main . hoge ) ;
20 }
21 }
22
23 //ExecMainを呼び出すクラス(テストクラスになるはず)
24 public class Sample {
25
26 public static void main ( String [ ] args ) {
27 String test1Result = run ( "first" ) ;
28 System . out . println ( "テスト1の処理結果は「" + test1Result + "」でした。" ) ;
29 String test2Result = run ( "second" ) ;
30 System . out . println ( "テスト2の処理結果は「" + test2Result + "」でした。" ) ;
31
32 }
33
34 public static String run ( String input ) {
35 String result = "" ;
36 try {
37 ProcessBuilder pb = new ProcessBuilder ( ) ;
38 pb . command ( "java" , "ExecMain" ) ;
39 Process p = pb . start ( ) ;
40 PrintStream out = new PrintStream ( p . getOutputStream ( ) ) ;
41 out . println ( input ) ;
42 out . flush ( ) ;
43 out . close ( ) ;
44
45 p . waitFor ( ) ;
46
47 InputStream is = p . getInputStream ( ) ;
48 InputStreamReader isr = new InputStreamReader ( is ) ;
49 BufferedReader br = new BufferedReader ( isr ) ;
50 String tmp ;
51 while ( ( tmp = br . readLine ( ) ) != null ) {
52 result = tmp ;
53 }
54 is . close ( ) ;
55 isr . close ( ) ;
56 br . close ( ) ;
57 } catch ( Exception e ) {
58 e . printStackTrace ( ) ;
59 }
60 return result ;
61 }
62 }
(各種close処理が中途半端なのはご勘弁ください。。。)
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。