Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

Get the Edge with a Professional Java IDE. 30-day free trial.

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) {

            System.out.print("-a"+a+z+"-b"+(b-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; 
                System.out.print("-a"+a+"-b"+b);
            }
        }
    }
    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!

Get the Java IDE that understands code & makes developing enjoyable. Level up your code with IntelliJ IDEA. Download the free trial.

Topics:
java ,quiz ,nested classes

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}