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

Microservices. Streaming data. Event Sourcing and CQRS. Concurrency, routing, self-healing, persistence, clustering...learn how Akka enables Java developers to do all this out of the box! Brought to you in partnership with Lightbend.

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!

Microservices. Streaming data. Event Sourcing and CQRS. Concurrency, routing, self-healing, persistence, clustering...learn how Akka enables Java developers to do all this out of the box! Brought to you in partnership with Lightbend. 

Topics:
java ,quiz ,nested classes

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}