Java Quiz: Nested Classes

DZone 's Guide to

Java Quiz: Nested Classes

What happens when you try to compile and run the following program?

· Java Zone ·
Free Resource

Starting today, every week, we'll be bringing you a new quiz/puzzler/brain-teaser to help keep you on top of your Java game. Here's your first question: 

What Happens When You Try to Compile and Run the Following Program?

class Outer {

    private int a = 12;
    private int b = 6;

    Outer() {

        NestedA nestedA = new NestedA(4);
        a = a -12;
    class NestedA {

        NestedA(int y){

            NestedB nestedB = new NestedB(y);
            nestedB.methodB(y, a);
            a= a + b - y;
        private void methodA(int z) {

        class NestedB {

            NestedB(int i) {
                a = a - i; b = b +i;
            private void methodB(int x, int z) {
                if(x < 5) {
                    a = a+x+b;
                b = b+z; 
    public static void main(String[] args) {

        NestedA nestedA = new Outer().new NestedA(2);

Submit your answer here (no compiling!):

Was this too difficult? Too easy? What other topics would you like to see Sar test you on? Let us know your thoughts in the comments!

java, nested classes, quiz

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}