#Design_Patterns
#Singleton_Design_Pattern
👉Problem
Application needs one, and only one, instance of an object. Additionally, lazy initialization and global access are necessary.
@javaCode☕️
#Singleton_Design_Pattern
👉Problem
Application needs one, and only one, instance of an object. Additionally, lazy initialization and global access are necessary.
@javaCode☕️
#Java_Interview_Question
88) Why java uses the concept of string literal?
To make Java more memory efficient (because no new objects are created if it exists already in string constant pool).
@javaCode☕️
88) Why java uses the concept of string literal?
To make Java more memory efficient (because no new objects are created if it exists already in string constant pool).
@javaCode☕️
☕️JAVA Language Community
#Java_Interview_Question 88) Why java uses the concept of string literal? To make Java more memory efficient (because no new objects are created if it exists already in string constant pool). @javaCode☕️
More details:
Java String literal is created by using double quotes. For Example:
String s="welcome";
Each time you create a string literal, the JVM checks the string constant pool first. If the string already exists in the pool, a reference to the pooled instance is returned. If string doesn't exist in the pool, a new string instance is created and placed in the pool. For example:
String s1="Welcome";
String s2="Welcome";//will not create new instance
❗️Note: String objects are stored in a special memory area known as string constant pool.
@javaCode☕️
Java String literal is created by using double quotes. For Example:
String s="welcome";
Each time you create a string literal, the JVM checks the string constant pool first. If the string already exists in the pool, a reference to the pooled instance is returned. If string doesn't exist in the pool, a new string instance is created and placed in the pool. For example:
String s1="Welcome";
String s2="Welcome";//will not create new instance
❗️Note: String objects are stored in a special memory area known as string constant pool.
@javaCode☕️
#Java_Interview_Question
89)How many objects will be created in the following code?
String s = new String("Welcome");
Two objects, one in string constant pool and other in non-pool(heap).
@javaCode☕️
89)How many objects will be created in the following code?
String s = new String("Welcome");
Two objects, one in string constant pool and other in non-pool(heap).
@javaCode☕️
#Design_Patterns
#Singleton_Design_Pattern
👉Discussion
Make the class of the single instance object responsible for creation, initialization, access, and enforcement. Declare the instance as a private static data member. Provide a public static member function that encapsulates all initialization code, and provides access to the instance.
The client calls the accessor function (using the class name and scope resolution operator) whenever a reference to the single instance is required.
Singleton should be considered only if all three of the following criteria are satisfied:
▪️Ownership of the single instance cannot be reasonably assigned
▪️Lazy initialization is desirable
▪️Global access is not otherwise provided for
If ownership of the single instance, when and how initialization occurs, and global access are not issues, Singleton is not sufficiently interesting.
The Singleton pattern can be extended to support access to an application-specific number of instances.
The "static member function accessor" approach will not support subclassing of the Singleton class. If subclassing is desired, refer to the discussion in the book.
Deleting a Singleton class/instance is a non-trivial design problem. See "To Kill A Singleton" by John Vlissides for a discussion.
@javaCode☕️
#Singleton_Design_Pattern
👉Discussion
Make the class of the single instance object responsible for creation, initialization, access, and enforcement. Declare the instance as a private static data member. Provide a public static member function that encapsulates all initialization code, and provides access to the instance.
The client calls the accessor function (using the class name and scope resolution operator) whenever a reference to the single instance is required.
Singleton should be considered only if all three of the following criteria are satisfied:
▪️Ownership of the single instance cannot be reasonably assigned
▪️Lazy initialization is desirable
▪️Global access is not otherwise provided for
If ownership of the single instance, when and how initialization occurs, and global access are not issues, Singleton is not sufficiently interesting.
The Singleton pattern can be extended to support access to an application-specific number of instances.
The "static member function accessor" approach will not support subclassing of the Singleton class. If subclassing is desired, refer to the discussion in the book.
Deleting a Singleton class/instance is a non-trivial design problem. See "To Kill A Singleton" by John Vlissides for a discussion.
@javaCode☕️
#Java_Interview_Question
90) What is the basic difference between string and stringbuffer object?
String is an immutable object. StringBuffer is a mutable object.
@javaCode☕️
90) What is the basic difference between string and stringbuffer object?
String is an immutable object. StringBuffer is a mutable object.
@javaCode☕️
#Java_Interview_Question
91) What is the difference between StringBuffer and StringBuilder ?
StringBuffer is synchronized whereas StringBuilder is not synchronized.
@javaCode☕️
91) What is the difference between StringBuffer and StringBuilder ?
StringBuffer is synchronized whereas StringBuilder is not synchronized.
@javaCode☕️
#Design_Patterns
#Singleton_Design_Pattern
👉Structure
Make the class of the single instance responsible for access and "initialization on first use". The single instance is a private static attribute. The accessor function is a public static method.
@javaCode☕️
#Singleton_Design_Pattern
👉Structure
Make the class of the single instance responsible for access and "initialization on first use". The single instance is a private static attribute. The accessor function is a public static method.
@javaCode☕️
#Java_Interview_Question
92) How can we create immutable class in java ?
There are many immutable classes like String, Boolean, Byte, Short, Integer, Long, Float, Double etc. In short, all the wrapper classes and String class is immutable. We can also create immutable class by creating final class that have final data members.
@javaCode☕️
92) How can we create immutable class in java ?
There are many immutable classes like String, Boolean, Byte, Short, Integer, Long, Float, Double etc. In short, all the wrapper classes and String class is immutable. We can also create immutable class by creating final class that have final data members.
@javaCode☕️
#Java_Interview_Question
93) What is the purpose of toString() method in java ?
The toString() method returns the string representation of any object. If you print any object, java compiler internally invokes the toString() method on the object. So overriding the toString() method, returns the desired output, it can be the state of an object etc. depends on your implementation.
@javaCode☕️
93) What is the purpose of toString() method in java ?
The toString() method returns the string representation of any object. If you print any object, java compiler internally invokes the toString() method on the object. So overriding the toString() method, returns the desired output, it can be the state of an object etc. depends on your implementation.
@javaCode☕️
☕️JAVA Language Community
#Singleton_Design_Pattern @javaCode☕️
#Design_Patterns
#Singleton_Design_Pattern
👉Example
The Singleton pattern ensures that a class has only one instance and provides a global point of access to that instance. It is named after the singleton set, which is defined to be a set containing one element. The office of the President of the United States is a Singleton. The United States Constitution specifies the means by which a president is elected, limits the term of office, and defines the order of succession. As a result, there can be at most one active president at any given time. Regardless of the personal identity of the active president, the title, "The President of the United States" is a global point of access that identifies the person in the office.
@javaCode☕️
#Singleton_Design_Pattern
👉Example
The Singleton pattern ensures that a class has only one instance and provides a global point of access to that instance. It is named after the singleton set, which is defined to be a set containing one element. The office of the President of the United States is a Singleton. The United States Constitution specifies the means by which a president is elected, limits the term of office, and defines the order of succession. As a result, there can be at most one active president at any given time. Regardless of the personal identity of the active president, the title, "The President of the United States" is a global point of access that identifies the person in the office.
@javaCode☕️
#Java_Interview_Question
94)What is nested class?
A class which is declared inside another class is known as nested class. There are 4 types of nested class member inner class, local inner class, annonymous inner class and static nested class.
@javaCode☕️
94)What is nested class?
A class which is declared inside another class is known as nested class. There are 4 types of nested class member inner class, local inner class, annonymous inner class and static nested class.
@javaCode☕️
☕️JAVA Language Community
#Java_Interview_Question 94)What is nested class? A class which is declared inside another class is known as nested class. There are 4 types of nested class member inner class, local inner class, annonymous inner class and static nested class. @javaCode☕️
More details:
👉Java Inner Class
Java inner class or nested class is a class i.e. declared inside the class or interface.
We use inner classes to logically group classes and interfaces in one place so that it can be more readable and maintainable.
Additionally, it can access all the members of outer class including private data members and methods.
❗️Syntax of Inner class
class Java_Outer_class{
//code
class Java_Inner_class{
//code
}
}
👉Advantage of java inner classes
There are basically three advantages of inner classes in java. They are as follows:
1) Nested classes represent a special type of relationship that is it can access all the members (data members and methods) of outer class including private.
2) Nested classes are used to develop more readable and maintainable code because it logically group classes and interfaces in one place only.
3) Code Optimization: It requires less code to write.
@javaCode☕️
👉Java Inner Class
Java inner class or nested class is a class i.e. declared inside the class or interface.
We use inner classes to logically group classes and interfaces in one place so that it can be more readable and maintainable.
Additionally, it can access all the members of outer class including private data members and methods.
❗️Syntax of Inner class
class Java_Outer_class{
//code
class Java_Inner_class{
//code
}
}
👉Advantage of java inner classes
There are basically three advantages of inner classes in java. They are as follows:
1) Nested classes represent a special type of relationship that is it can access all the members (data members and methods) of outer class including private.
2) Nested classes are used to develop more readable and maintainable code because it logically group classes and interfaces in one place only.
3) Code Optimization: It requires less code to write.
@javaCode☕️
#Design_Patterns
#Singleton_Design_Pattern
👉Check list
1️⃣Define a private static attribute in the "single instance" class.
2️⃣Define a public static accessor function in the class.
3️⃣Do "lazy initialization" (creation on first use) in the accessor function.
4️⃣Define all constructors to be protected or private.
5️⃣Clients may only use the accessor function to manipulate the Singleton.
@javaCode☕️
#Singleton_Design_Pattern
👉Check list
1️⃣Define a private static attribute in the "single instance" class.
2️⃣Define a public static accessor function in the class.
3️⃣Do "lazy initialization" (creation on first use) in the accessor function.
4️⃣Define all constructors to be protected or private.
5️⃣Clients may only use the accessor function to manipulate the Singleton.
@javaCode☕️
#Java_Interview_Question
95) Is there any difference between nested classes and inner classes?
Yes, inner classes are non-static nested classes i.e. inner classes are the part of nested classes.
@javaCode☕️
95) Is there any difference between nested classes and inner classes?
Yes, inner classes are non-static nested classes i.e. inner classes are the part of nested classes.
@javaCode☕️
#Java_Interview_Question
96) Can we access the non-final local variable, inside the local inner class?
No, local variable must be constant if you want to access it in local inner class.
@javaCode☕️
96) Can we access the non-final local variable, inside the local inner class?
No, local variable must be constant if you want to access it in local inner class.
@javaCode☕️
#Java_Interview_Question
👉Types of Nested classes
There are two types of nested classes non-static and static nested classes.The non-static nested classes are also known as inner classes.
1️⃣Non-static nested class(inner class)
1)Member inner class
2)Annomynous inner class
3)Local inner class
2️⃣Static nested class
---------------------------------
👉Description:
1) Member Inner Class :
A class created within class and outside method.
2) Anonymous Inner Class :
A class created for implementing interface or extending class. Its name is decided by the java compiler.
3) Local Inner Class :
A class created within method.
4) Static Nested Class :
A static class created within class.
5) Nested Interface :
An interface created within class or interface.
@javaCode☕️
👉Types of Nested classes
There are two types of nested classes non-static and static nested classes.The non-static nested classes are also known as inner classes.
1️⃣Non-static nested class(inner class)
1)Member inner class
2)Annomynous inner class
3)Local inner class
2️⃣Static nested class
---------------------------------
👉Description:
1) Member Inner Class :
A class created within class and outside method.
2) Anonymous Inner Class :
A class created for implementing interface or extending class. Its name is decided by the java compiler.
3) Local Inner Class :
A class created within method.
4) Static Nested Class :
A static class created within class.
5) Nested Interface :
An interface created within class or interface.
@javaCode☕️
#Java_Interview_Question
97) What is nested interface ?
Any interface i.e. declared inside the interface or class, is known as nested interface. It is static by default.
@javaCode☕️
97) What is nested interface ?
Any interface i.e. declared inside the interface or class, is known as nested interface. It is static by default.
@javaCode☕️
#Java_Interview_Question
98) Can a class have an interface?
Yes, it is known as nested interface.
@javaCode☕️
98) Can a class have an interface?
Yes, it is known as nested interface.
@javaCode☕️
#Design_Patterns
#Singleton_Design_Pattern
👉Rules of thumb
1️⃣Abstract Factory, Builder, and Prototype can use Singleton in their implementation.
2️⃣Facade objects are often Singletons because only one Facade object is required.
3️⃣State objects are often Singletons.
4️⃣The advantage of Singleton over global variables is that you are absolutely sure of the number of instances when you use Singleton, and, you can change your mind and manage any number of instances.
5️⃣The Singleton design pattern is one of the most inappropriately used patterns. Singletons are intended to be used when a class must have exactly one instance, no more, no less. Designers frequently use Singletons in a misguided attempt to replace global variables. A Singleton is, for intents and purposes, a global variable. The Singleton does not do away with the global; it merely renames it.
6️⃣When is Singleton unnecessary? Short answer: most of the time. Long answer: when it's simpler to pass an object resource as a reference to the objects that need it, rather than letting objects access the resource globally. The real problem with Singletons is that they give you such a good excuse not to think carefully about the appropriate visibility of an object. Finding the right balance of exposure and protection for an object is critical for maintaining flexibility.
7️⃣Our group had a bad habit of using global data, so I did a study group on Singleton. The next thing I know Singletons appeared everywhere and none of the problems related to global data went away. The answer to the global data question is not, "Make it a Singleton." The answer is, "Why in the hell are you using global data?" Changing the name doesn't change the problem. In fact, it may make it worse because it gives you the opportunity to say, "Well I'm not doing that, I'm doing this" – even though this and that are the same thing.
@javaCode☕️
#Singleton_Design_Pattern
👉Rules of thumb
1️⃣Abstract Factory, Builder, and Prototype can use Singleton in their implementation.
2️⃣Facade objects are often Singletons because only one Facade object is required.
3️⃣State objects are often Singletons.
4️⃣The advantage of Singleton over global variables is that you are absolutely sure of the number of instances when you use Singleton, and, you can change your mind and manage any number of instances.
5️⃣The Singleton design pattern is one of the most inappropriately used patterns. Singletons are intended to be used when a class must have exactly one instance, no more, no less. Designers frequently use Singletons in a misguided attempt to replace global variables. A Singleton is, for intents and purposes, a global variable. The Singleton does not do away with the global; it merely renames it.
6️⃣When is Singleton unnecessary? Short answer: most of the time. Long answer: when it's simpler to pass an object resource as a reference to the objects that need it, rather than letting objects access the resource globally. The real problem with Singletons is that they give you such a good excuse not to think carefully about the appropriate visibility of an object. Finding the right balance of exposure and protection for an object is critical for maintaining flexibility.
7️⃣Our group had a bad habit of using global data, so I did a study group on Singleton. The next thing I know Singletons appeared everywhere and none of the problems related to global data went away. The answer to the global data question is not, "Make it a Singleton." The answer is, "Why in the hell are you using global data?" Changing the name doesn't change the problem. In fact, it may make it worse because it gives you the opportunity to say, "Well I'm not doing that, I'm doing this" – even though this and that are the same thing.
@javaCode☕️