2.nested Classes in Java
2.nested Classes in Java
class OuterClass
{
...
class NestedClass
{
...
}
}
Static Nested Classes
• A static nested class is associated with its outer class similar to class
methods and variables.
• Static nested classes are accessed using the enclosing class name:
OuterClass.StaticNestedClass
• For example, to create an object for the static nested class, use this
syntax:
OuterClass.StaticNestedClass nestedObject =
new OuterClass.StaticNestedClass();
// a static nested class
// outer class
public class StaticNestedClassDemo
class OuterClass {
{ public static void main(String[] args)
{
static int outer_x = 10; OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();
int outer_y = 20;
private static int outer_private = 30; nestedObject.display();
static class StaticNestedClass }
{ }
void display()
{
// can access static member of outer class
System.out.println("outer_x = " + outer_x);
// can access display private static member of outer class
System.out.println("outer_private = " + outer_private);
// The following statement will give compilation error
// as static nested class cannot directly access non-static members
// System.out.println("outer_y = " + outer_y);
}
}
}
Inner Classes(Non-static nested classes)
• An inner class is associated with an instance of its enclosing class
and has direct access to that object's methods and fields.
• To instantiate an inner class, we must first instantiate the outer class. Then,
create the inner object within the outer object.
• Syntax:
OuterClass.InnerClass innerObject =
outerObject.new InnerClass();
void display(){
Inner in=new Inner();
in.msg();
}
public static void main(String args[]){
TestMemberOuter1 obj=new TestMemberOuter1();
obj.display();
}
}
Local Inner Classes
• Local classes are classes that are defined in a block, which is a group of
zero or more statements between balanced braces.
For example, we can define a local class in a method body, a for loop, or an
if clause.
• A local class has access to the members of its enclosing class.
• The scope of local inner class is restricted to the block they are defined in.
• Local inner class cannot be instantiated from outside the block where it is
created in.
• Till JDK 7,Local inner class can access only final local variable of the
enclosing block. However From JDK 8, it is possible to access the non-final
local variable of enclosing block in local inner class.
More points of Local Inner Class
Anonymous classes also have the same restrictions as local classes with
respect to their members:
• We cannot declare static initializers or member interfaces in an anonymous class.
• An anonymous class can have static members provided that they are constant
variables.
Outerclass.class
Outerclass$Nestedclass.class
class Main
{
public static void main(String[] args)
{
A ob = new A() // annoymous inner class
{
void show()
{
System.out.println("Overridden " +x);
}
};
ob.show();
}
}
2. Anonymous Inner class that implements an interface
We can also have an anonymous inner class that implements an
interface
interface A
{
int x=12;
void show();
}
class Main
{
public static void main(String[] args)
{
A ob = new A() // annoymous inner class
{
public void show()
{
System.out.println("Overridden " +x);
}
};
ob.show();
}
}
3. Anonymous class that extends abstract class
A.13
{
void show()
{
B. 0
class Test
{
static int a=13;
void display()
{
System.out.println(a);
C. Compile time
error
}
}
Test ob=new Test();
D.Runtime error
ob.display();
}
}
public class Main
{
public static void main(String[] args)
{
Example obj=new Example();
obj.show();
}
}
Q5(Output??)
interface Example
{
A. 100 0
int a=100; B. 0 200
void show();
} C. Compile time
class Main error
{
public static void main(String[] args) D. 100 200
{
Example ob = new Example() // anonymous inner class
{
static final int b=200;
public void show()
{
System.out.println(a+" "+b);
}
};
ob.show();
}
}
Q6(Output??)
abstract class ABC
{
OP1: A B
abstract void show1(); OP2: Compile time error
abstract void show2();
} OP3:Runtime error
class Main
{
OP4:Blank output
public static void main(String[] args)
{
ABC ob = new ABC() // anonymous inner class
{
void show1(){ System.out.print("A "); }
void show2(){ System.out.print("B "); }
};
ob.show1();
ob.show2();
}
}
Q7
Which of the following is true regarding anonymous inner class?
static class Y
{
static int y = x++;
static class Z
{
static int z = y++;
}
}
}
public class Main
{
public static void main(String[] args)
{
System.out.print(X.x+" ");
System.out.print(X.Y.y+" ");
System.out.print(X.Y.Z.z);
}
}
A. 100 101 102
B. 100 100 100
C. 100 0 0
D. Compile time error
Q11
Which of the following is true about anonymous inner classes?
A. You can create ‘n’ number of objects to anonymous inner
classes.
B. Anonymous inner classes will not have the name.
C. You can instantiate anonymous inner classes only once.
D. Both B and C
class A
{
Q12(Output??)
static String s = "A";
class B
A. A B A
{ B. A A B
String s = "B";
void methodB() C. A B B
{
System.out.print(s); D. B A A
}
}
}
public class Main
{
public static void main(String[] args)
{
A a = new A();
System.out.print(a.s+" ");
A.B b = a.new B();
System.out.print(b.s+" ");
b.methodB();
}
}
Q13(Output??)
class A
{ A. Method Two
void methodOne()
{
class B
B. Compile time error
{
void methodTwo() C. Blank Output
{
}
System.out.println("Method Two");
D. Runtime error
}
}
void methodThree()
{
new B().methodTwo();
}
}
public class Main
{
public static void main(String[] args)
{
A a = new A();
a.methodThree();
}
}
Q14(Output??)
class X
{
A. Y
void methodX() B. Compile time error
{
class Y C. Runtime error
{
static void methodY() D. Blank Output
{
System.out.println("Y");
}
}
Y.methodY();
}
}
public class Main
{
public static void main(String[] args)
{
new X().methodX();
}
}
Which of the following members of outer class are
accessible inside a member inner class?[Q15]