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 do you know about the garbage collector in java?
Explain the difference between collection api and stream api in java8?
What is the purpose of tostring() method in java?
What are java annotations?
What does int [] mean in java?
What is a website container?
What is the use of toarray () in java?
what do you mean by classloader?
What is the relationship between clipping and repainting under awt?
What is the difference between preparedstatement and statement in java?
Is java jre still free?
What is the buffer limit?
how we can use debug in myeclipse 6.0 in order solve the problems that exist in our program when there are 900 to 1000 pages in a web application
What is multi-catch block in java?
Explain inner classes ?