inner-classes

What are the uses of inner classes in Java? Are nested classes and inner classes the same? [duplicate]

≡放荡痞女 提交于 2019-12-18 11:09:08
问题 This question already has answers here : Closed 9 years ago . Possible Duplicate: Java inner class and static nested class What are the uses of inner classes in Java? Are nested classes and inner classes the same? 回答1: No, they're not the same: an inner class is non- static . JLS 8.1.3 Inner Classes and Enclosing Instances An inner class is a nested class that is not explicitly or implicitly declared static. Consult also the following diagram from Joe Darcy: Joseph D. Darcy's Oracle Weblog -

Why compile time constants are allowed to be made static in non static inner classes?

血红的双手。 提交于 2019-12-18 06:50:59
问题 Suppose we have code as below. public class Outer{ class Inner{ public static final String s = "abc"; } static class Nested{ public static final SomeOtherClass instance = new SomeOtherClass(); } } I understand to instantiate object of non static inner classes an object of Outer class is needed . static means class related and for accessing it an object is not required to be instantiated. Non static inner class can only be used once we have an object of Outer class instantiated. Having any

Why compile time constants are allowed to be made static in non static inner classes?

梦想与她 提交于 2019-12-18 06:50:04
问题 Suppose we have code as below. public class Outer{ class Inner{ public static final String s = "abc"; } static class Nested{ public static final SomeOtherClass instance = new SomeOtherClass(); } } I understand to instantiate object of non static inner classes an object of Outer class is needed . static means class related and for accessing it an object is not required to be instantiated. Non static inner class can only be used once we have an object of Outer class instantiated. Having any

Inner classes with the same name as an outer class?

随声附和 提交于 2019-12-18 04:41:58
问题 Constraints: I have a maven source code generator that I wrote that is creating POJO classes from some data files that have nested namespaces. I want each namespace to be nested as an inner class. In some cases out of my control I end up with inner classes that are the same simple name as the outermost class. All the classes must be public scope as this is for a type safe wrapper over something like a properties file, but hierarchical.. I can't change the names otherwise I am changing the

Can an outer class access the members of inner class?

左心房为你撑大大i 提交于 2019-12-18 04:05:42
问题 The inner class is the class defined inside a class, and the inner class can be declared as public, private, protected. If the inner class defined as private and protected, can outer class access the members of inner class? and can inner class access members of outer class? 回答1: If the inner class defined as private and protected, can outer class access the members of inner class? Yes. These qualifiers will only affect the visibility of the inner class in classes that derive from the outer

protected/public Inner Classes

两盒软妹~` 提交于 2019-12-18 03:24:19
问题 Can someone please explain to me what is the difference between protected / public Inner classes? I know that public inner classes are to avoid as much as possible (like explained in this article). But from what I can tell, there is no difference between using protected or public modifiers. Take a look at this example: public class Foo1 { public Foo1() { } protected class InnerFoo { public InnerFoo() { super(); } } } ... public class Foo2 extends Foo1 { public Foo2() { Foo1.InnerFoo innerFoo

Instantiate inner class object from current outer class object

社会主义新天地 提交于 2019-12-17 20:25:28
问题 I am wondering if the following is valid in Java: class OuterClass { OuterClass(param1, param2) { ...some initialization code... } void do { // Here is where the doubt lays OuterClass.InnerClass ic = this.new InnerClass(); } class InnerClass { } } Basically what I am trying to achieve here is to instantiate an inner class object from the current instance of the outer class, not a new instance, the current one. I believe this comes handy is when the constructor of the outer class is not empty

Lambda vs anonymous inner class performance: reducing the load on the ClassLoader?

对着背影说爱祢 提交于 2019-12-17 20:08:36
问题 I would like to know how big of a benefit lambdas have in Java 8. I agree that it might be more readable sometimes to use lambdas, but does it have really such of a big impact on the performance side? Or is it mainly focused as syntactic sugar? I prefer anonymous inner classes sometimes; do I really lose many benefits when I don't use lambda all the time? The only ?big? performance gain, it seems to me, is that we don't actually create classes that the class loader has to load at the start of

Usage of inner class

馋奶兔 提交于 2019-12-17 10:42:07
问题 I can understand what inner class is and how to write program. My question is in what situation do programmers really need inner class? 回答1: Sometimes there is some functionality which is best represented as an object, but which is only meaningful within the context of another object, which does not necessarily need to be exposed to the outside world, and which can benefit from having access to the parent classes data (so as to not violate encapsulation). The best example that I can think of

Can I access new methods in anonymous inner class with some syntax?

匆匆过客 提交于 2019-12-17 09:43:33
问题 Is there any Java syntax to access new methods defined within anonymous inner classes from outer class? I know there can be various workarounds, but I wonder if a special syntax exist? For example class Outer { ActionListener listener = new ActionListener() { @Override void actionPerformed(ActionEvent e) { // do something } // method is public so can be accessible public void MyGloriousMethod() { // viva! } }; public void Caller() { listener.MyGloriousMethod(); // does not work! } } MY OWN