Found 7442 Articles for Java

How to prevent Serialization to break a Singleton Class Pattern?

Arjun Thakur
Updated on 23-Jun-2020 14:38:06

679 Views

A Singleton pattern states that a class can have a single instance and multiple instances are not permitted to be created. For this purpose, we make the constructor of the class a private and return a instance via a static method. But using serialization, we can still create multiple instance of a class. See the example below −Example - Breaking Singleton Live Demoimport java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; public class Tester{    public static void main(String[] args)    throws ClassNotFoundException, IOException{       A a = A.getInstance();       A b ... Read More

How to prevent Serialization to break a Singleton Class Pattern?

Arjun Thakur
Updated on 23-Jun-2020 14:38:06

679 Views

A Singleton pattern states that a class can have a single instance and multiple instances are not permitted to be created. For this purpose, we make the constructor of the class a private and return a instance via a static method. But using serialization, we can still create multiple instance of a class. See the example below −Example - Breaking Singleton Live Demoimport java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; public class Tester{    public static void main(String[] args)    throws ClassNotFoundException, IOException{       A a = A.getInstance();       A b ... Read More

How to prevent Reflection to break a Singleton Class Pattern?

Chandu yadav
Updated on 23-Jun-2020 14:43:37

898 Views

A Singleton pattern states that a class can have a single instance and multiple instances are not permitted to be created. For this purpose, we make the constructor of the class a private and return a instance via a static method. But using reflection, we can still create multiple instance of a class by modifying the constructor scope. See the example below −Example - Breaking Singleton Live Demoimport java.io.Serializable; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; public class Tester {    public static void main(String[] args) throws    InstantiationException, IllegalAccessException,    IllegalArgumentException, InvocationTargetException{       A a = A.getInstance();     ... Read More

How to prevent Reflection to break a Singleton Class Pattern?

Chandu yadav
Updated on 23-Jun-2020 14:43:37

898 Views

A Singleton pattern states that a class can have a single instance and multiple instances are not permitted to be created. For this purpose, we make the constructor of the class a private and return a instance via a static method. But using reflection, we can still create multiple instance of a class by modifying the constructor scope. See the example below −Example - Breaking Singleton Live Demoimport java.io.Serializable; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; public class Tester {    public static void main(String[] args) throws    InstantiationException, IllegalAccessException,    IllegalArgumentException, InvocationTargetException{       A a = A.getInstance();     ... Read More

How to prevent Cloning to break a Singleton Class Pattern?

George John
Updated on 23-Jun-2020 14:27:50

2K+ Views

A Singleton pattern states that a class can have a single instance and multiple instances are not permitted to be created. For this purpose, we make the constructor of the class a private and return a instance via a static method. But using cloning, we can still create multiple instance of a class. See the example below −Example - Breaking Singletonpublic class Tester{    public static void main(String[] args)    throws CloneNotSupportedException {       A a = A.getInstance();       A b = (A)a.clone();       System.out.println(a.hashCode());       System.out.println(b.hashCode());    } } ... Read More

How to prevent Cloning to break a Singleton Class Pattern?

George John
Updated on 23-Jun-2020 14:27:50

2K+ Views

A Singleton pattern states that a class can have a single instance and multiple instances are not permitted to be created. For this purpose, we make the constructor of the class a private and return a instance via a static method. But using cloning, we can still create multiple instance of a class. See the example below −Example - Breaking Singletonpublic class Tester{    public static void main(String[] args)    throws CloneNotSupportedException {       A a = A.getInstance();       A b = (A)a.clone();       System.out.println(a.hashCode());       System.out.println(b.hashCode());    } } ... Read More

How to prevent object of a class from garbage collection in Java?

Ankith Reddy
Updated on 23-Jun-2020 14:30:45

273 Views

If a object is no more referenced by a live reference then it becomes eligible for garbage collection. See the example below −Examplepublic class Tester{    public static void main(String[] args) {       test();    }    public static void test(){       A a = new A();    } } class A {}When test() method complete execution, the a object is no more referenced and is eligible for garbage collection. Java garbage collector will deallocate the object when it runs.To prevent garbage collection, we can create a static reference to an object and then ... Read More

How to prevent object of a class from garbage collection in Java?

Ankith Reddy
Updated on 23-Jun-2020 14:30:45

273 Views

If a object is no more referenced by a live reference then it becomes eligible for garbage collection. See the example below −Examplepublic class Tester{    public static void main(String[] args) {       test();    }    public static void test(){       A a = new A();    } } class A {}When test() method complete execution, the a object is no more referenced and is eligible for garbage collection. Java garbage collector will deallocate the object when it runs.To prevent garbage collection, we can create a static reference to an object and then ... Read More

Java program to check occurrence of each character in String

Chandu yadav
Updated on 05-Sep-2024 11:23:51

24K+ Views

To find the occurrence of each character in a string we can use the Map utility of Java. In Map a key cannot be duplicated so make each character of the string a key of Map and provide the initial value corresponding to each key as 1 if this character is not inserted in Map before. Now when a character repeats during insertion as a key in Map increases its value by one. Continue this for each character until all characters of the string get inserted. Problem Statement Write a program in Java to check the occurrence of each character in ... Read More

Java program to check occurrence of each character in String

Chandu yadav
Updated on 05-Sep-2024 11:23:51

24K+ Views

To find the occurrence of each character in a string we can use the Map utility of Java. In Map a key cannot be duplicated so make each character of the string a key of Map and provide the initial value corresponding to each key as 1 if this character is not inserted in Map before. Now when a character repeats during insertion as a key in Map increases its value by one. Continue this for each character until all characters of the string get inserted. Problem Statement Write a program in Java to check the occurrence of each character in ... Read More

Advertisements