Menu
Topics Index
...
`

Classes inside other class1952


If you need explanation Read this topic

If you need Answer Take test on this topic

Classes inside other class1952
What will be the output of the following program?
public class GuessIt {
    public static void main(String[] args) {
        Guess g = new Guess();
        g.G();
        Guess.CantGuess.ICanGuess.Hai hai = null;
        hai.print();
    }
}
class Guess {
    protected void G() {
        CantGuess cg = new CantGuess();
    }
    class CantGuess {
        public CantGuess() {
            System.out.print("CAB");
        }
        ICanGuess icg = new ICanGuess();
        GetICanGuess gicg = new GetICanGuess();
        class ICanGuess {
            public ICanGuess() {
                System.out.print("BAC");
            }
            Hai h = new Hai();
            public class Hai {
                public Hai() {
                    System.out.print("CBA");
                }
                public void print() {
                    System.out.print("print");
                }
            }
        }
        class GetICanGuess {
            public GetICanGuess() {
                System.out.print("ABC");
            }
        }
    }
}

A. CBABACABCCAB
B. CABCBABACABC
C. ABCBACCABCBA
D. Some other output
F. Compilation Error or Runtime Error
Topic: Access Modifiers In Java

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 F... here we have inner classes concept
we have two types of inner classes
* static inner classes
* non static inner classes

in this example we are deealing with non static inner classes..in this we have local inner classes,member,Anonymous

here we have member inner classes which must contains only non static members..in one case we can declare an varible as static if that  varible is final

class can have any number of inner classes

coming to the examle we are creating object for the Guess class so the that constrcutors of the inner classes get executed
next we are creating reference for the class Hai which is included in Guess-->CantGuess-->ICanGuess->Hai

here we area creating refernce fot the class Hai as hai..
next we are calling the methid print() here NullPointerException wil thrown Bcz we cant call an method in class wiht refernce..we must create Object for it

Posted by Uday Kumar    2014-10-21 12:45:16


f

Posted by Shubham Bansal    2014-10-21 13:08:17


This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Uday Kumar for the 'Popular Comment'. The 'lucky liker' is ShweYee Win. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2014-10-22 03:44:12


© meritcampus 2019

All Rights Reserved.

Open In App