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

The single app analytics solutions to take your web and mobile apps to the next level.  Try today!  Brought to you in partnership with CA Technologies

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!

CA App Experience Analytics, a whole new level of visibility. Learn more. Brought to you in partnership with CA Technologies.

Topics:
java ,quiz ,nested classes

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}