Friday, December 14, 2018

Singleton Pattern

Singleton pattern is the simplest pattern in java and is under the creational pattern. some key points to use in singleton pattern.
  • It is static in nature
  • private constructor
  • private static instance of the class
  • public static getter method
  • no parameters to the constructor
create singleton class
public class Singleton {

    private static Singleton object = null;

    private Singleton() {
    }
    public static Singleton getInstance() {
        if (object == null) {
            object = new Singleton();
        }
        return object;
    }
    public void showMessage() {
        System.out.println("Singleton Pattern");
    }
}
create demo class
public class Demo {
    public static void main(String[] args) {
        Singleton object = Singleton.getInstance();
        object.showMessage();
    }
}
but this classic implementation not thread safe. therefore using synchronized make sure that the one thread can execute the getInstance();at one time.
As this;
public class Singleton {

    private static Singleton object = null;

    private Singleton() {
    }
    public static synchronized Singleton getInstance() {
        if (object == null) {
            object = new Singleton();
        }
        return object;
    }

}

What is the difference between JVM, JDK, JRE

JVM is abbreviated as Java Virtual Machine , JVM is the main component of java architecture. JVM is written in C programming language. Java compiler produce the byte code for JVM. JVM reading the byte code verifying the byte code and linking the code with the ibrary.

JRE is abbreviated as Java Runtime Environment. it is provide environment at runtime. It is physically exist. It contain JVM + set of libraries(jar) +other files.

JDK is abbreviated as Java Development Kit . it is develop java applications. And also Debugging and monitoring java applications . JDK contain JRE +development tools(javac,java)

Tuesday, March 6, 2018

How to Refresh / Reload the Web Page



You can use JavaScript location.reload() method. This method accepts a boolean parameter. true or false. If the parameter is true; the page always reloaded from the server. If it is false; which is the default or with empty parameter browser reload the page from it's cache.

With true parameter


<button type="button" onclick="reloadPage();">Reload page</button> 
 <script type="text/javascript">
    function reloadPage(){
        location.reload(true);
    }
 </script> 

With defaultfalse parameter




<button type="button"  onclick="reloadpage()">Reload page</button>
<script="text/javascript">
function reloadpage() {
    location.reload();
}
</script>

Using jQuery

<button id="Reloadpage">Reload page</button>
        <script type="text/javascript">
        $('#Reloadpage').click(function() {
        location.reload();
         }); 
        </script>

Singleton Pattern

Singleton pattern is the simplest pattern in java and is under the creational pattern. some key points to use in singleton pattern. It is...