abishkar bhattarai
abishkar bhattarai

Reputation: 7651

What is it called and when to use it in java for interface

In spring framework spring modules validation jar file i have seen following code

package org.springmodules.validation.valang.predicates;


    public interface Operator
    {
        public static interface IsNotLowerCaseOperator
            extends Operator
        {
        }

        public static interface IsLowerCaseOperator
            extends Operator
        {
        }

        public static interface IsNotUpperCaseOperator
            extends Operator
        {
        }
        }



public interface OperatorConstants
{
public static final Operator IS_NOT_LOWER_CASE_OPERATOR = new Operator.IsNotLowerCaseOperator() {

    }
;
   public static final Operator IS_LOWER_CASE_OPERATOR = new Operator.IsLowerCaseOperator() {

    }
;
    public static final Operator IS_NOT_UPPER_CASE_OPERATOR = new Operator.IsNotUpperCaseOperator() {

    }
;
public abstract class AbstractPropertyPredicate
    implements Predicate
{
public AbstractPropertyPredicate( Operator operator)
    {  
        setOperator(operator);
    }

 public final Operator getOperator()
    {
        return operator;
    }
     private Operator operator;
     }

While accesing following is used

 if(getOperator() instanceof Operator.IsNotLowerCaseOperator)

I have never seen this style of coding?What it is called ?When to use it? Any help please?

Upvotes: 0

Views: 69

Answers (1)

GerritCap
GerritCap

Reputation: 1626

It is called anonymous inner classes, they are of the format

Instance i = new Instance() {
... extra methods, etc.... possibly implementing abstract methods as inherited from Instance
}

They are used for simple one time needed implementations of a specific interface or abstract class or an "normal" class.

Most of the time used for event handlers in swing as in

new ActionListener() {
    public actionPerformed(ActionEvent e) {...}
    }
}

In this case it is used to simulated an enum without enums being available in earlier java versions

Upvotes: 1

Related Questions