enfrptes

sendassignment@tutorspoint.com

Member Inner Class Homework Help

What is Member inner class in Java?

If we define a class in another class just as a member-like variable and functions of those classes, then we call such a class a member inner class.

Example:

Class outer1

{

Int x;

Class Inner1

{

Int I;

Public void fun In ()

{

System. Out. Prinln(“ inside fun In() of Inner 1”);

X=25;

Fun Ou();

}

Public void fun Ou ()

{

X = x+1;

Inner in = new Inner1 ();

in.i =25;

System. Out. Prinln( in . i);

}

Public static void main (string args [ ])

{

+ Outer1. Inner1 oi = new Outer 1 (). New Inner 1 ();

Oi. Fun In ();

}

}

+, this is the syntax for creating objects of inner classes from static functions of outer classes. Since a class can contain static and non-static members, we may think of defining static members in the inner classes. But, the rule says that non-static member inner classes should not have static members.

Explanation:

In the above program, if the main method is present in some other class test within the same path, even then we follow the same (+) syntax for creating an object of the inner class. Thus we can create objects of the inner class from its outer class as well as from some other class that is outside the other class.

If we declare an inner class as private, then it is possible to create an object of an inner class only in its outer class outside the outer class. It is not possible to create an object of the inner class. An inner class can extend any class which is available to its outer class. An inner class can extend an outer class but there is no use for it because, since the inner class is a member of the outer class.