Teachnique
      CourseRoadmaps
      Login

      OverviewHistoryFeaturesJava 8 - New Featuresvs C++Virtual Machine(JVM)JDK vs JRE vs JVMHello World ProgramEnvironment SetupBasic SyntaxVariable TypesData TypesType CastingUnicode SystemBasic OperatorsCommentsStreamsNew Date-Time API

      Loop ControlDecision Makingif-else Statementswitch statementfor loopfor each Loopwhile Loopdo...while Loopbreak Statementcontinue Statement

      OOPs (Object-Oriented Programming) ConceptsObject and ClassesClass AttributesClass MethodsMethodsVariable ScopesConstructorsAccess ModifiersInheritanceAggregationPolymorphismOverridingMethod OverloadingDynamic BindingStatic BindingInstance Initializer BlockAbstractionEncapsulationInterfacesPackagesInner classesStatic ClassesAnonymous ClassesSingleton ClassWrapper ClassesEnum Class

      Number ClassBoolean classCharacter ClassArraysMath Class

      File ClassCreating FilesWrite To FilesReading FileDelete FilesDirectory OperationsFiles and I/O

      ExceptionsTry Catch BlockTry with ResourcesMultiple Catch BlocksNested Try BlockFinally BlockThrows and Throw | Throw an ExceptionException PropagationBuilt-in ExceptionsCustom Exception

      MultithreadingThread Life CycleCreating a ThreadStarting a ThreadJoining ThreadsNaming a Thread with ExamplesScheduling Threads with ExamplesThread PoolsMain ThreadThread PriorityDaemon ThreadThreadGroup ClassJVM Shutdown Hook

      Thread SynchronizationBlock SynchronizationStatic SynchronizationInter Thread CommunicationThread DeadlockInterrupting ThreadThread ControlReentrant Monitor

      NetworkingSocket ProgrammingURL ProcessingURL ClassURLConnection ClassHttpURLConnection ClassSocket Class with ExamplesGenerics

      Collections FrameworkCollection Interface

      List InterfaceArrayList Class

      Queue InterfaceArrayDeque Class

      Map InterfaceSortedMap Interface

      Set InterfaceSortedSet Interface

      Data Structures Enumeration Interface BitSet Class

      How to Use Iterator?How to Use Comparator?How to Use Comparable?

      RecursionRegular ExpressionsSerializationString ClassJava Arrays - Class

      Feedback

      Submit request if you have any questions.

      Course
      Static Classes

      Java Tutorial

      This Java tutorial is tailored for newcomers, offering a journey from basic principles to complex Java programming techniques. Completing this tutorial equips you with a solid understanding of Java, preparing you for advanced learning. You'll emerge ready to tackle the challenges of becoming a top-tier software engineer, with the skills to innovate and excel in the vast world of software development.

      Static Classes

      
      In Java concept of static class is introduced under concept of inner classes, which are specially designed for some delicate functionality in a class.

      Java Static Classes

      Static classes in Java are allowed only for inner classes which are defined under some other class, as static outer class is not allowed which means that we can't use static keyword with outer class.
      Static classes are defined the same as other inner classes in Java only with a static keyword in front of its name. These classes have some unique characteristics that make them differ from other non-static inner classes.

      Features of Java Static Classes

      The following are the features of static classes in Java:
      • Static class do not need to create an instance of outer containing class in order to create its own instance.
      • Static class can access members(variables/methods) of outer containing class only if they are static in nature.Which means that a static nested class does not have access to the instance variables and methods of the outer class.

      Syntax of Java Static Class

      The syntax of static nested class is as follows
      class MyOuter {
      static class Nested_Demo {
      }
      }
      Instantiating a static nested class is a bit different from instantiating an inner class. The following programs show how to use a static nested class in multiple cases.

      Example of Java Static Class

      In this example, we've created a class Outer and an inner static class as NestedDemo within it. In main() method, we're using static method of static class directly without any reference as main is part of Outer class.
      package com.tutorialspoint;
      
      public class Outer {
      static class NestedDemo {
      public static void print() {
      System.out.println("This is my nested class");
      }
      }
      public static void main(String args[]) {
      NestedDemo.print();
      }
      }
      If you compile and execute the above program, you will get the following result
      Output
      This is my nested class

      Java Static Class: More Examples

      Example 1

      In this example, we've created a class Outer and an inner static class as NestedDemo within it. As main method is in different class, we're accessing the static inner class using Outer class.
      package com.tutorialspoint;
      
      public class Tester {
      public static void main(String[] arguments) {
      Outer.NestedDemo.print();
      }
      }
      
      class Outer {
      static class NestedDemo {
      public static void print() {
      System.out.println("This is my nested class");
      }
      }
      }
      If you compile and execute the above program, you will get the following result
      Output
      This is my nested class

      Example 2

      In this example, we've created a class Outer and an inner static class as NestedDemo within it. Now as static class has instance method and main method is in different class, we're accessing the static inner class using Outer class object.
      package com.tutorialspoint;
      
      public class Tester {
      public static void main(String[] arguments) {
      new Outer.NestedDemo().print();
      }
      }
      
      class Outer {
      static class NestedDemo {
      public void print() {
      System.out.println("This is my nested class");
      }
      }
      }
      If you compile and execute the above program, you will get the following result
      Output
      This is my nested class