
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to instantiate a static inner class with reflection in Java?
A static inner class can be instantiated without the need for an instance of the outer class. In general, an Inner class is a part of nested class, called Non-static nested classes in Java. The types of inner classes are member inner class, anonymous inner class, and local inner class.
We can instantiate a static inner class with reflection using InnerClass.class.newInstance(). If we need an instance of the outer class to instantiate a non-static inner class, we can specify it before a new operator.
Example
import java.lang.reflect.*; public class InnerclassWithReflectionTest { public static void main(String args[]) { try { InnerClass inner = (InnerClass) InnerClass.class.newInstance(); inner.test(); } catch(Exception e) { e.printStackTrace(); } } // inner class static class InnerClass { public void test() { System.out.println("Welcome to TutorialsPoint !!!"); } } }
Output
Welcome to TutorialsPoint !!!
- Related Articles
- How to instantiate member inner class in Java?\n
- How to access Static variable of Outer class from Static Inner class in java?
- Can you extend a static inner class in Java?
- How to instantiate a class in C#?
- Inner class in Java
- Reflection Array Class in Java
- Local inner class in Java
- Static class in Java
- What is a static class in Java?
- How to call a non-static method of an abstract class from a static method in java?
- What is an Inner class in Java?
- List methods of a class using Java Reflection
- What is the difference between static classes and non-static inner classes in Java?
- How to implement an interface using an anonymous inner class in Java?
- How to create a static class in C++?

Advertisements