Commit 0b8ed9c0 authored by Suresh Kumar's avatar Suresh Kumar

Singleton

parent 6c965654
package singleton.com;
public class Student {
static Student st = new Student();
private Student(){
System.out.println("Simple Singleton Created ");
}
static Student getInstance(){
return st;
}
}
package singleton.com;
public class Test {
public static void main(String [] args){
Student s1 = Student.getInstance();
Student s2 = s1;
System.out.println(s1==s2);
}
}
package fetching.com;
class Eager{
private static Eager obj=new Eager();//Early, instance will be created at load time
private Eager(){
System.out.println("Eager Singleton Instantiation");
}
public static Eager getEager(){
return obj;
}
public static void main(String [] args ){
Eager e1 = Eager.getEager();
}
}
package fetching.com;
public class Lazy{
private static Lazy l = null;
private Lazy()
{
System.out.println("Lazy Singleton being initialized");
}
public static Lazy getInstance()
{
if(l == null)
l = new Lazy();
return l;
}
public static void main(String [] args){
Lazy l1 = Lazy.getInstance();
}
}
\ No newline at end of file
package hashcode.com;
public class Hashcode {
public static void main(String [] args){
String a = "suresh";
String b ="suresh";
if(a.equals(b)){
System.out.println("a and b both variable are equals and their respective values are : " + " " + a.hashCode() +
" And " + b.hashCode() );
}
String c = "suresh";
String d = "kumar";
if(!c.equals(d)){
System.out.println("c and d both variable are un_equals and their respective values are : " + " " + c.hashCode() +
" And " + d.hashCode() );
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment