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

Which is fastest collection in java?

0 Answers  


What is boolean example?

0 Answers  


What is the difference between heap memory and stack memory?

0 Answers   Aspiring Minds,


What are the actions that can occur when a thread enters blocked state?

0 Answers  


What does n mean?

0 Answers  






What is stack explain?

0 Answers  


How many types of array are there?

0 Answers  


what is enumset?

0 Answers  


Explain the difference between getAppletInfo and getParameterInfo?

1 Answers  


program to validate the IP address? Validity range should be 0 to 255

1 Answers   Huawei,


What is the difference between a switch statement and an if statement?

0 Answers  


Explain the difference between association, aggregation and inheritance relationships.

0 Answers  


Categories