Menu
Topics Index
...
`

Creating static and non static classes


If you need explanation Read this topic

If you need Answer Take test on this topic

Creating static and non static classes
Which of the following instantiated objects defined in the instantiatedTest() method are valid instantiations?
public class ValidOrNot {
public class X {    }
public static class Y {    }
    public void instantiatedTest() {
        new ValidOrNot().new X(); //M
        new ValidOrNot.Y(); //N
        new X(); //O
        new ValidOrNot.X(); //P
    }
}

A. Only M
B. Only O
C. M and O
D. N and O
E. M, N, Oand P
Topic: Static and Non Static Variables - Static and Non Static Methods

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

Ans Is c... X is an member of validornot class.. there to two options to create object of x.. 1) create directly as object of validornot is directly available to member function 2) create using the object of validornot

Posted by Krishna Sai    2013-09-05 09:30:15


ans is E..Inside a method,inner class can be instantiated directly or using outer class reference...

Posted by Soumya Tata    2013-09-05 09:36:48


ans is E
M is valid because [ new ValidOrNot().new X() ] is similar to
ValidOrNot obj=new ValidOrNot();
obj.new X(); which is accepted.

N is valid because inner class can be referenced via the outer class (but not necessary when pointing from the outer class or inner class. But outer_class.inner_class() is compulsory when pointing outside the outer class).

O is accepted as it is reference is made within the outer class.

P is valid (ans is same as that of N one)

Posted by Vallab Srinath    2013-09-05 12:01:05


Congratulations Soumya Tata. You are this dose winner. Please send us your mobile details in a private message and we will process the recharge asap.

Posted by Merit Campus    2013-09-06 03:29:49


© meritcampus 2019

All Rights Reserved.

Open In App