How do I add a class to a list?

squirrel :

I have a class SnakeCaseFormatter by which

class SnakeCaseFormatter implements TextFormatter { etc...

and

List<TextFormatter> formatter = new ArrayList<TextFormatter>();

when I use

formatter.add(SnakeCaseFormatter);

I get an error which says: "SnakeCaseFormatter cannot be resolved to a variable".

May I know how to correct this?

Jeroen Steenbeeke :

SnakeCaseFormatter is the name of your class, i.e. a design for an object. To get an actual object, you need to instantiate it:

SnakeCaseFormatter myFormatter = new SnakeCaseFormatter();

You can then add this instance to your list:

formatter.add(myFormatter);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=110806&siteId=1