-6

I had recently asked question on stackoverflow, although I got the solution, but I am not able to understand the way the code is written there. This is the code

public class User {
@NotNull(message = "First name can't be blank", groups = {Step1.class, FinalStep.class})
private String firstName;

@NotNull(message = "Last name can't be blank", groups = {Step1.class, FinalStep.class})
private String lastName;

@NotNull(message = "Email can't be blank", groups = {Step1.class, FinalStep.class})
private String emailAddress;

@NotNull(message = "Please provide a valid address", groups = {Step2.class, FinalStep.class}) // this one also
private Address address;


public interface Step1 {} // this one 
public interface Step2 {}// this one 
public interface FinalStep {} // this one
}

the original post is Link to original post

  • The first question which comes in my mind why interface is declared in the class.
  • Second what is the advantage of doing this type of coding
  • and last where I can use this approach

Finally if it is useful, can you name this approach if not then any link which can explain about this more clearly.

Vivek Singh
  • 646
  • 3
  • 10
  • 25
  • If you don't have enough knowledge, then please don't down vote. – Vivek Singh Aug 22 '17 at 09:31
  • or atleast give a proper link or reason for down voting – Vivek Singh Aug 22 '17 at 09:31
  • 3
    It's a marker interface. These annotations enable validation. It's called [JSR-303 - Java Bean Validation](https://en.wikipedia.org/wiki/Bean_Validation). When you need to validate user input. In the future, I suggest asking one question - not three. – Elliott Frisch Aug 22 '17 at 09:35
  • My downvote is because it it not clear what you are asking. (Not because I don't understand Java!) – Stephen C Aug 22 '17 at 09:42
  • What you have shown us is code not a "technique" or "approach". If you want us to explain a specific "technique" or "approach" in that code, you need to explain which one you are talking about. (Off the top of my head, I can quickly identify 4 possibilities ...) – Stephen C Aug 22 '17 at 09:48
  • Because I didn't knew the technique/approach name till now, and in the last three line, I had clearly mention what problem I was facing. Any way I got the solution. – Vivek Singh Aug 22 '17 at 09:51
  • And at last I even don't want any up voting just solution/help of my queries. – Vivek Singh Aug 22 '17 at 09:53

1 Answers1

1

I found this searching SO: Why should we declare an interface inside a class?

Thus, when dealing with some hierarchy, you can describe a "nested" interface, which will be implemented by the wrapping class's subclasses.

In the JDK, the most significant example would be Map.Entry inner interface, defined within Map interface and implemented by various ways by HashMap, LinkedHashMap etc...

cst1992
  • 3,823
  • 1
  • 29
  • 40
  • 1
    @VivekSingh see [this link](https://stackoverflow.com/help/how-to-ask). It may help with some of the downvotes. – cst1992 Aug 22 '17 at 09:42