how to make the double-tone class ? as we have singletone
class..?



how to make the double-tone class ? as we have singletone class..?..

Answer / 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

More Core Java Interview Questions

Why does the integer quotient -0/3 yield 0, but the double quotient -0.0/3.0 yields – 0.0?

0 Answers  


Explain about global variables in Java?

3 Answers  


what invokes a threads run() method? : Java thread

0 Answers  


Is it possible for yielded thread to get chance for its execution again ?

0 Answers  


What does percent mean in java?

0 Answers  






Does a function need a return?

0 Answers  


What are different exception types exceptions available in java ?

0 Answers  


Difference between Array and vector?

9 Answers  


what are class,constructor and primitive data types?

2 Answers   IBM,


What are instance variables?

0 Answers  


Can we use different return types for methods when overridden?

0 Answers  


when asub class inherits a super class and overrides a public method of super class in sub class(public method in super class). why these methods needs to be public in sub class. (otherwise compile time error).

3 Answers  


Categories