1

I Have a UserEntity class which implements the IUserEntity interface.

In the UserEntity class I have a static map:

private static Map<IUserEntity.IIdentifiable, IUserEntity> staticUserEntityMap = new HashMap<>(); 

In the IUserEntity interface I would like to write a method like that:

public Collection<IUserEntity> getUsers(); 

And in the class :

public static Collection<IUserEntity> getUsers(){ return staticUserEntityMap.values(); } 

But I can't declare static methods in the interface and I can't change the method signature in the UserEntity class.

How can I do this ?

7
  • What's wrong with public Collection<IUserEntity> getUsers(){ return staticUserEntityMap.values(); } Commented Dec 26, 2015 at 13:32
  • Just write a non static method calling the static one if you can't change any signature. Commented Dec 26, 2015 at 13:38
  • Interfaces cannot have static methods without a method body. That's it. In general interface and static dont work well together, which makes sense. Commented Dec 26, 2015 at 13:41
  • 1
    Also you might want to look at stackoverflow.com/questions/512877/… (Java 8 allows static methods in interfaces). Commented Dec 26, 2015 at 13:44
  • 1
    @Gaël but not without implementation, right? Commented Dec 26, 2015 at 13:45

3 Answers 3

3

In Java 8 you can have default implementations in interface but I believe that will not solve your problem. Instead of changing the method signatures you can create a separate static method and call it with the class name within getUsers implementation. e.g.

Create new method:

public static Collection<IUserEntity> getUsersStatic() { return staticUserEntityMap.values(); } 

Call this method from getUsers:

public Collection<IUserEntity> getUsers() { return UserEntity.getUsersStatic(); } 
Sign up to request clarification or add additional context in comments.

1 Comment

Huummmm, Then i have to do this for all CRUD methods if i want to implements those ? And each UserEntity object have access to those "static" methods isn't bad ?
2

I'd refer you to this question, and the docs:

In addition to default methods, you can define static methods in interfaces. (A static method is a method that is associated with the class in which it is defined rather than with any object. Every instance of the class shares its static methods.) This makes it easier for you to organize helper methods in your libraries; you can keep static methods specific to an interface in the same interface rather than in a separate class.

You could, however, implement the field in the interface as well, as that would allow you to implement your simple static method. Downside would be that the Map would become public. It'd look like this:

public interface IUserEntity { // ... static Map<IUserEntity.IIdentifiable, IUserEntity> staticUserEntityMap = new HashMap<>(); static Collection<IUserEntity> getUsers(){ return staticUserEntityMap.values(); } } 

2 Comments

Okay Thanks, I think it's a better structure idea then.
There's no way to let the staticMap protected or private ?
0

you can create abstract SkeletonUserEntity(or AbstractUserEntity) class where you would define this getUser method and all another general methods. All classes have to implemeent IUserEntity you extends from SkeletonUserEntity

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.