public static List<Movie> movies;
static {
movies = new ArrayList<>();
movies.add(new Movie(1, "Fprd vs Ferrari", "Movie on Racing", "abcd", "xyz"));
movies.add(new Movie(2, "F2", "Comedy Movie", "Venkatesh", "Tamanna"));
movies.add(new Movie(3, "Titanic", "Movie", "Hero", "Heroine"));
Movie max = Collections.max(movies, Comparator.comparingInt(Movie::getId));
int maxId = max.getId();
}
public Result insert(Http.Request request) {
JsonNode jsonNode = request.body().asJson();
if (jsonNode == null) {
return badRequest("insufficient movie information");
}
Movie movie = Json.fromJson(jsonNode, Movie.class);
movies.add(movie);
return ok(Json.toJson(movie));
}
This is insert method. now I want access that maxId variable to insert method. anyone can tell me how this one will cleared to get increment the id
You can't. It's a local variable.
That is also not a static method, it's a static block. It is executed the first time the class is loaded in the memory, and not after that. If you want to keep that value, change your code to this:
EDIT
What you can do is extract the last two lines to an actual method (either static or instance method):
and call this as you would any other method.