how to make the double-tone class ? as we have singletone
class..?
Answer Posted / sagar kumar
public class DoubletonTest {
public static void main(String[] args) {
Helloo h1=Helloo.getHello(1);
Helloo h2=Helloo.getHello(2);
Helloo h3=Helloo.FIRST;
Helloo h4=Helloo.SECOND;
System.out.println(h1);
System.out.println(h2);
System.out.println(h3);
System.out.println(h4);
System.out.println(h1==h3);//true
System.out.println(h2==h4);//true
}
}
class Helloo{
private Helloo(){
}
final static public Helloo FIRST=new Helloo();
final static public Helloo SECOND=new Helloo();
static public Helloo getHello(int val){
if (val<1 || val>2) {
throw new IllegalArgumentException(" "+val);
}
if (val==1) {
return FIRST;
}
return SECOND;
}
}
Is This Answer Correct ? | 1 Yes | 0 No |
Post New Answer View All Answers
Why we cannot override static method?
What is the java reflection api? Why it’s so important to have?
What is a key in java?
What is square root in java?
What is an array length?
Should a main method be compulsorily declared in all java classes?
What sorting algorithm does javascript use?
Why are data types important?
What is a static method in java?
What lambda means?
how to create multithreaded program? Explain different ways of using thread? When a thread is created and started, what is its initial state? : Java thread
What is a lambda expression ? What's its use ?
What is type inference in java8?
How do you reverse a string in java?
What is java in simple terms?