-3

I want a class to check if an input is valid, and all the valid input is record in a text file.

So in construction, it read in the text file and put all valid inputs into a HashSet. Then I have static function to receive an input and check if the input is in the HashSet.

Code structure is like following:

public class Validator {
    HashSet validInputs;

    public Validator() {
        //read in
    }

    public static boolean validate(String in) {
        //check and return
    }
}

Then in other class, I need to use Validator class to validate strings. Code is like:

...
String a = XXX;
boolean valid = Validator.validate(a);
...

I haven't tested the code, but I have two questions:

  1. Does it work? Is the valid input text file read in?
  2. When will the class read in the text file?
  3. Will Validator read the text file every time I call the function validate()?
DrXCheng
  • 3,992
  • 11
  • 49
  • 72

4 Answers4

5

Does it work? Is the valid input text file read in?

No, that won't work.

Your method should be an instance method so that it can access other instance members.

public boolean validate(String in) {
    //check and return
}

When will the class read in the text file?

You have to construct the class before you can use it. The text file is read in the constructor.


Will Validator read the text file every time I call the function validate()?

No. The constructor is called when you call new Validator().

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
1

It won't. validInputs will be tied to an instance of Validator and you won't be able to reference it from within a static method. What you need is a static block:

public final class Validator {
    private static HashSet validInputs;

    private Validator() {}

    static {
        //read in
    }

    public static boolean validate(String in) {


     //check and return
    }
}

If you do it this way:

When will the class read in the text file? -  When the class is loaded by the Class loader.
Will Validator read the text file every time I call the function validate()? -  No, only when the class is loaded.
Jeshurun
  • 22,940
  • 6
  • 79
  • 92
1

No, you won't be able to access your validInputs until you create an instance of Validator using new Validator()

  1. No, your file won't be read in at the time of use.
  2. The file will be read when you create a new instance of Validator.
  3. No. It will only be read in when you have new Validator().

If you wish to be able to access this method staticly, consider using a Singleton.

cklab
  • 3,761
  • 20
  • 29
1

You could use a singleton and a non-static validate-method:

public class Validator {
    private static Validator instance = null;

    /**
     * Do not use new Validator; use Validator.getInstance() instead.
     */
    private Validator() {
        // read in
    }

    public static Validator getInstance() {
        if(instance == null) {
            instance = new Validatorr();
        }
        return instance;
    }

    public boolean validate(String str) {
        // check and return
    }
}

Everywhere where you need to validate something use:

Validator.getInstance().validate()

NOTE: A side effect of the singleton pattern implemented this way is that if the Validator is not used, the Validator is not created and in is not read. This may or may not be what you want.

Kasper van den Berg
  • 8,951
  • 4
  • 48
  • 70