what is the difference between static class and singleton class? can we create static class?
Answers were Sorted based on User's Feedback
Static class always a inner class. and static class can create multiple objects.all object have different reference value.
But on the other hand SingleTon class can have only single object because every object reference variable have same reference value.
Is This Answer Correct ? | 3 Yes | 0 No |
Answer / pratima thakur
1.We can create static class object like the below example.
class OuterClass
{
void outerMethod()
{
System.out.println("Inside in outerclass");
}
static class InnerClass
{
void innerMethod()
{
System.out.println("Inside in innerclass");
}
}
public static void main(String args[])
{
InnerClass iclass=new InnerClass();
InnerClass iclass1=new InnerClass();
iclass.innerMethod();
}
}
2.Single tone class implementaion
public class MySingleTon {
private static MySingleTon myObj;
/**
* Create private constructor
*/
MySingleTon(){
}
/**
* Create a static method to get instance.
*/
public static MySingleTon getInstance(){
if(myObj == null){
myObj = new MySingleTon();
}
return myObj;
}
public void getSomeThing(){
// do something here
System.out.println("I am here....");
}
public static void main(String a[]){
MySingleTon st = MySingleTon.getInstance();
System.out.println(st);
MySingleTon st1 = MySingleTon.getInstance();
System.out.println(st1);
st.getSomeThing();
}
}
Is This Answer Correct ? | 1 Yes | 0 No |
How to Sort Strings which are given in List and display in ascending order without using java api.
Does constructor be static?
What are operators and its types?
Write POJO class as a key to hashmap???
What class allows you to read objects directly from a stream?
String is a immutable objects . it means that string does not change........... But it will be chang......... { String s="kapil"; String s1="raj"; String s=s1; then print(.......) The String has been changed .. how it is possible and why its called immutable objects
What are the drawbacks for singleton class?
Why generics are used in java?
what is stringtolennizer with example?
How many objects are created when we create String class object using new operator?
Is treeset sorted in java?
What is treeset in java collection?