Definition of Static inner classes:

If we define any inner class as static then it is known as static inner class.

 

Properties of static inner classes:

  • We can define any access specifier before a static inner class.
  • Static inner classes can have static members as well as non–static members.
  • Static members of a class are directly available to the static and non-static members of that class but non-static members of a class are not directly available to the static members of that class.
  • In a static inner class, even though if we define a non-static function, it is still static because it is in a static class. I.e. it is in the static domain.
  • From static inner class, we cannot access any non-static members of the outer class directly. In order to do so, we need to create an object of outer class explicitly.

 

Example Program:

Class S outer 1

{

Int x;

Static int y;

Static class Sinner1

{

Public static void funS1 ()

{

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

Y = y+1;

}

Public void funS2 ()

{

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

X=x+1; // error

}

}

Public void fun Ou ()

{

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

Sinner1 s = new Sinner1 ();

  1. Fun s2 ();

Sinner1. funS1 (); \\ here funS1 () is static

}

Public static void main (string args [ ])

{

Y=y+1;

Sinner1 s = new Sinner1 ();

  1. Fun s2 ();

Sinner1. funS1 ();

}

}

  • Syntax for creating object of static inner class from outside the outer class.
  • This is the way in which we refer static and non-static members of the static inner class from a class outside the outer class.

Static Inner Classes Homework Help

Tutorspoint.com offers best java assignment help, java project help to students of university & colleges in the USA, UK & Australian countries at an affordable price with high-quality work from java experts