Posts

Showing posts from 2020

Enum private Constructor

enum has constructor. This constructor is private. Why do we need a private constructor? because enum has finite values.  If the constructor is public , anyone can change the values of enum. It can increase the values  This would extend the initial values of enum.  It is a compile time error if the constructor of an enum type is declared as public or protected.  If no access modifier is specified for the constructor of an enum type, The constructor is by default private. package com.simple   public enum  BankAccount    {      AXIS  ("Axis Bank Account"),     DENA  ("Dena Bank Account"),     HDFC  ("Hdfc Bank Account");     private final String desc ;     BankAccount    ( String   desc) {        this.desc = desc;       }     public String getDesc(){       return desc;    } Here we have a Ba...

Predicate The Functional interface in java

What is a functional interface?  A Functional Interface is an Interface which allows only one abstract method within the interface scope. To use a normal interface there are two steps. 1. Declare one interface with abstract method. 2. Provide  body of Interface  by implementing on a class. Same happen in case of functional interface. Important difference in functional interface there is only one abstract method declaration. While implementing we use the benefit of single abstract method , we write anonymous function for single abstract method. This functional interface helps us to write lambda in java. public interface MyFunctionalnterface < T > { String funPart (T t) ; } public class UseFunctional { public static void main(String [] arg) { String str =" Functional World !"; M yFunctionalnterface<String> callFun; callFun= (xyz)-> { return str + xyz; }; System.out.println(callFun. funPart (" in java ")); } } Som...

Aggregate function with spring data

Image
I was working on a small project. Requirement was different in term of object mapping. There is simple way to map  java object with database table. Aggregate function in query is not mapped with entity class. We don't have direct mapping attribute in JPA annotation to map aggregate function with entity class. Spring data support this using interface. In ORM query we use aggregate function e.g. count , sum etc. the output of these aggregate function cannot directly map with the Entity class. To collect the outcome of aggregate methods. We need to map using interface in java and provide the Interface class with mapping method.      public List<ITeacherReport> getTeacherReport(String teacherName); Our requirement is to count solved question per month by teacher from answer table. We have below environment configuration. Thymeleaf (for view,  this is template engine) JDK 1.8 Maven Springboot framework  MySql  for database operation Open the start....

Why can't we override System class methods in Java

Image
Below is a link to the source code of the system class of Java's LANG package. Which I have taken from the internet. http://hg.openjdk.java.net/jdk7/jdk7/jdk/file/cf44386c8fe3/src/share/classes/java/lang/System.java There are two main points 1. The system class is a final class. It cannot be extended. If we are declaring someone as a finalist then it means that it will not be expanded further. You are in a situation from which you should not move forward. 2. Perhaps you never tried to look at the System class. If you would have seen by opening the system class, then you would know that all its methods are static. Now you know about the static method, then you will also know that these methods will be loaded with the class. Static methods  are common to all object of same class. If a method is static, it cannot be overridden. I am assuming here that you know to override a method of  class, S...

Java Persistence API with Spring Data

Image
Let's try to build simple java application with the help of Spring Tool Suites IDE, Spring-Data connecting and MySql's relation database. You are free to choose any IDE (STS, Eclipse, Intellij etc).  We are building simple application which is storing User information into MySQL Database table. Our Requirements are simple IDE (any IDE, STS, Eclipse or Intellij) JDK 1.8 later  Maven 3.2 Create a Maven project using spring initializr   Open the Spring Initializr ( https://start.spring.io/  ).  This is the best way to start the project. Type the https://start.spring.io on url browser  The Initializr provides quick solution to place dependency and create project structure. Our requirement is JPA and MySql dependencies. below image shows the Initializr with required dependency selection: Following  pom.xml  file  will be generated having   spring-boot-starter-data  and   mysql-con...