malangi
malangi

Reputation: 2770

Java coding style - calling a method inside a class

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..

Upvotes: 1

Views: 459

Answers (4)

MattGrommes
MattGrommes

Reputation: 12354

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.

Upvotes: 0

jackbot
jackbot

Reputation: 3021

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.

Upvotes: 0

mR_fr0g
mR_fr0g

Reputation: 8722

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.

Upvotes: 0

Bozho
Bozho

Reputation: 597402

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

FieldVerifier.this.validUserName(boxText);

Upvotes: 1

Related Questions