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

What is the difference between I ++ and ++ I in java?

728


What do you understand by garbage collection in Java? Can it be forced to run?

751


What is the nested interface?

792


what is meant by Byte code concept in Java?

789


Can we overload the constructors?

753


What is mean by exception?

748


How many characters is 2 bytes?

734


Can we use synchronized block for primitives?

818


What is strings in java?

792


How do you add an element to a hashset in java?

691


what is difference between equals and ==?

799


Can we define static methods inside interface?

726


What is the difference between hashmap and hashtable in java?

769


What is an object class?

768


Can we have multiple classes in single file ?

816