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
What is equlas() and hashcode() contract in java? Where does it used?
Can you use this() and super() both in a constructor?
What is variable and its types?
Explain the meaning of java applet.
What package is math in java?
What is r in java?
Can we have try block without catch block?
How do you make an arraylist empty in java?
What is reflexive association?
Which programming language is most secure?
What classes of exceptions may be caught by a catch clause in java programming?
What is the maximum size of list in java?
What do you mean by JVM?
What do you understand by weak reference?
What is covariant return type?