1

I want to call a validation method inside a shared gwt class that i have created to store the validation logic (for user entered text fields)

 suggestBox.addKeyUpHandler( new KeyUpHandler() {
            public void onKeyUp(KeyUpEvent event) {
                if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) { 
                    String boxText = suggestBox.getText();
                    if (new FieldVerifier().validUserName(boxText)) { //inner class used to instanciate the FieldVerifier class where validUserName(String ..) lives

now i could do this with a properly instanciated FieldVerifier class (rather than a inner class as above) - or indeed, perhaps make it abstract. but i have the suspicion i am missing something (ie. must be an elegant way of doing it).

looked on google code search, but didnt come across anything particularly helpful..

1
  • Is suggestBox.addKeyUpHandler( new KeyUpHandler() { in a method that is part of FieldVerifier ? Commented Mar 10, 2010 at 22:44

4 Answers 4

1

I'm not sure I got it, but try:

FieldVerifier.this.validUserName(boxText);
Sign up to request clarification or add additional context in comments.

Comments

0

If you made the validUserName() method of FieldVerifier static then you could just call FieldVerifier.validUerName() directly, without having to instantiate a FieldVerifier object. If it's a fairly small class, though, the overhead of creating a new object is likely to be minimal.

2 Comments

I did start with making it static - but eclipse seemed to suggest i should only 'call it in a static context' so i wasnt sure if this was a wonky practice. it seems though (from the comments above) that instantiating objects for uses such as this seems to be OK in java?
If the FieldVerifier class isn't consuming a bunch of resources in its constructor (say if it's just initialising a few variables or something) then you're not going to notice anything by instantiating a new object just to call validUserName() so it's fine to do that.
0

i could do this with a properly instanciated FieldVerifier class (rather than a inner class as above)

Your use of FieldVerifier is not an inner class. It is indeed 'properly instanciated'. KeyUpHandler is an example of an anonymous inned class.

Comments

0

If I understand what you're trying to do, I would make validUserName() a static method. It doesn't appear to require or change any state; you just pass in something, run some verification logic on it, then return a boolean. This case is when you want to start looking at using statics.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.