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

Answer Posted / 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       View All Answers


Please Help Members By Posting Answers For Below Questions

Have you ever used hashtable and dictionary?

578


How to check if linked list contains loop in java?

459


How objects of a class are created if no constructor is defined in the class?

580


Which is faster string or stringbuilder?

521


What is use of super keyword?

567






Can a main method be declared final?

588


What is method overloading in JAVA? Why is it not present in C ?

592


What are the 4 types of research methods?

515


Explain about oops concepts.

636


What is string english?

545


What is a treeset class?

560


Give some features of interface?

586


When do you call copy constructor?

556


How can you add and remove nodes in jtree?

614


What are the benefits of java?

573