what is the difference between static class and singleton class? can we create static class?

Answers were Sorted based on User's Feedback



what is the difference between static class and singleton class? can we create static class?..

Answer / sumitpalsingh

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

what is the difference between static class and singleton class? can we create static class?..

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

Post New Answer

More Core Java Interview Questions

what is predefined function in java?

0 Answers  


What is the requirement of thread in java?

0 Answers  


where the static methods will live ,on stack ? can you explain brefly

1 Answers  


Convert a binary search tree to a sorted doubly linked list inplace.

1 Answers   Amazon,


can we access the method of class without creating the object of the class

3 Answers  






Is java a super set of javascript?

0 Answers  


did interface can implementation method ? i know its not possible but my interviewer said its possible ..but how..? any one have idea ???

7 Answers   IBM,


take any 4 input numbers. like 2345.. wanted out put is All 16 combinations of the number 2345.. for example- taking input as 4565 output- 5654 4556 4655..

2 Answers   Emphasis,


What does microservices mean?

0 Answers  


What are different types of arrays?

0 Answers  


How will you invoke any external process in java?

0 Answers  


What is an argument java?

0 Answers  


Categories