Take 2 classes.i.e., Class A,Class B. In Class B declare constructor as Private.But I have to access Class A using the constructor in Class B. How is it possible?One interviewer told that Through getInstance() method it's possible to access Class A.e Even Class B declare as Private. How is it possible? Explain with an example.

Questions by GeekAdmin   answers by GeekAdmin

Showing Answers 1 - 5 of 5 Answers

Hi.

Yes its true when u have method in ClassB as getInstance() which return the new ClassB(). Here it is about creating singleton object of ClassB which returns only one instance at give point.

  Was this answer useful?  Yes

Suneet Jain

  • Nov 20th, 2006
 

class A {

    public static void main(String[] as) {

        B b = B.getInstance(); // get the instance of Class B and store it in variable b

    }

}

class B {

    private B() {}

    public static B getInstance() {

        return new B();

    }

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions