Is there a any way to generate inner classes in javapoet library. I can generate classes with constructors and methods. But i can't figure out how to create inner classes
To instantiate an inner class, you must first instantiate the outer class. Then, create the inner object within the outer object with this syntax: OuterClass outerObject = new OuterClass(); OuterClass. InnerClass innerObject = outerObject.
You can only make nested classes either static or non-static. If you make a nested class non-static then it also referred to as Inner class.
Creating an inner class is quite simple. You just need to write a class within a class. Unlike a class, an inner class can be private and once you declare an inner class private, it cannot be accessed from an object outside the class.
Note: We can not have a static method in a nested inner class because an inner class is implicitly associated with an object of its outer class so it cannot define any static method for itself.
Use the TypeSpec.Builder#addType
method. See nestedClasses() test in TypeSpecTest.java
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With