Intermediate quizzes – Java nested classes

The following Java exercise(s) are designed for intermediate level programmers. If the level is too hard, then I recommend to select an easier one or you might consider reading my article about this topic, which offers a theoretical explanation including more exercises. Read More: Java Nested And Inner Classes


Quiz 1: Using nested classes in Java

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

class Outer {

	public int x = 1;
	private int y = 3;

	Outer() {
		x += 3;
		y += 2;
	Outer(int i) {
		x += i;
		System.out.print("-x" + x);
	class Inner {
		int x = 3;
		private void method(){
			x -= 1;
			System.out.print("-x" + x + "-x" + y);
	public static void main(String[] args){
		Inner inner = new Outer(2).new Inner();

Select the correct answer.

Sar Maroof is a professional software development teacher, gives master classes and publishes technical articles. He is also an expert software developer and worked for several big as well as small companies and later as a freelancer.
 Subscribe to Sar Maroof's RSS feeds!
 Mail this page to a friend!
 Bookmark this page!

1 Comment

Add a Comment
  1. You’re the grteseat! JMHO

Leave a Reply