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


Please Help Members By Posting Answers For Below Questions

Why we cannot override static method?

561


What is the java reflection api? Why it’s so important to have?

557


What is a key in java?

503


What is square root in java?

588


What is an array length?

500






Should a main method be compulsorily declared in all java classes?

534


What sorting algorithm does javascript use?

530


Why are data types important?

541


What is a static method in java?

539


What lambda means?

545


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

535


What is a lambda expression ? What's its use ?

565


What is type inference in java8?

579


How do you reverse a string in java?

583


What is java in simple terms?

563