Java更新类之间的全局变量

时间:2015-03-06 19:25:04

标签: java global-variables

我有一个名为randomWord的公共静态字符串。在另一个类运行一个方法后,我想要"值"它要改变示例:具有公共静态字符串randomWord的类变量; Class 1做了一些东西并设置了Variables.randomWord = orange。 2级需要获得新的randomWord。什么是更新类变量中randomWord的可能方法?

提前致谢

    public final class Variables
{
    public static String randomWord;
}

Class ^

    public static String pickWord()
        throws IOException
{    
    File words = new File("wordList.txt");
    String wordToArray;
    String[] arrWord = new String[3863];   
    Scanner sc = new Scanner(words);
    Random rWord = new Random();
    int i = 0;

    do
    {
        sc.nextLine();
        wordToArray = sc.next();
        arrWord[i] = wordToArray;
        i++;    
    }while(sc.hasNext());

    Arrays.toString(arrWord);
    int idx = rWord.nextInt(arrWord.length);

    Variables.randomWord = (arrWord[idx]);
    System.out.println(Variables.randomWord);
    return Variables.randomWord;
}

在我更改值的地方,将单词的.txt文档设置为数组,然后从数组中随机获取一个单词。 1级

String selectedWord = Variables.randomWord;
            System.out.println("Got your word, get ready!" + selectedWord);
            return selectedWord;

我需要新值Class 2

3 个答案:

答案 0 :(得分:0)

你可以做到这一点。创建一个类,添加公共静态变量并修改变量Variables.randomWord。但是让这个类最终变得更明智,使静态var变为私有并使用静态函数来获取和设置这个变量(封装)

答案 1 :(得分:0)

一个工作示例(假设您正确设置了wordList.txt文件路径)

Variables.java:     包com.avk.randomword;

public final class Variables {

    private static String randomWord;

    public static String getRandomWord(){
        return randomWord;
    }

    public static void setRandomWord( String randomWord ){
        Variables.randomWord = randomWord; 
    }
}

Class1.java:

package com.avk.randomword;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class Class1 {

    public String pickWord(){    
        String              wordToArray;
        String              randomWord;
        ArrayList<String>   arrWord         = new ArrayList<String>();
        Random              rWord           = new Random();

        try{
            File words          = new File("wordList.txt");
            Scanner sc          = new Scanner(words);

            while( sc.hasNext() ) {
                wordToArray = sc.next().trim();
                if( wordToArray.length() > 0 ){
                    arrWord.add( wordToArray );
                }
            }

        }catch( IOException e){
            e.printStackTrace();
        }

        if( arrWord.size() <= 0 ){
            System.out.println( "No words found in array arrWord" );
            return null;
        }
        int idx = rWord.nextInt( arrWord.size() -1 );

        randomWord = arrWord.get(idx);

        Variables.setRandomWord( randomWord );
        System.out.println( "Class1 set Variables.randomWord to: "  + Variables.getRandomWord());
        return Variables.getRandomWord();
    }   

}

等级2:

package com.avk.randomword;

public class Class2 {

    // Constructor
    public Class2(){
        System.out.println( "This is Class2 getting the randomWord from final class Variables: " + Variables.getRandomWord() );
    }

}

Main.java:

package com.avk.randomword;

public class Main {

    public static void main(String[] args) {
        Class1 myClass1 = new Class1();
        myClass1.pickWord();

        Class2 myClass2 = new Class2();

    }

}

如果wordList.txt看起来像这样:

apple
pear

orange
banana

可能的正确输出是:

Class1 set Variables.randomWord to: pear
This is Class2 getting the randomWord from final class Variables: pear

希望这会有所帮助。

答案 2 :(得分:0)

我修好了。

需要在第1课中添加

Variables.randomWord = Variables.randomWord = (arrWord[idx]);