inner-classes

“variable is accessed from within inner class needs to be declared final” error

为君一笑 提交于 2019-12-31 07:00:11
问题 I've got this error while trying to use a local member of one class in inner class inside. I know that declare it as final will solve the issue but I read that Java 8 should handle it automaticlly, I'm using Intellij with Java 8 and it still does not compile. Is there any other way to fix it without declare it as final? thanks. 回答1: I know that declare it as final will solve the issue but I read that Java 8 should handle it automatically. Java 8 will handle it if the variable is effectively

“variable is accessed from within inner class needs to be declared final” error

放肆的年华 提交于 2019-12-31 07:00:08
问题 I've got this error while trying to use a local member of one class in inner class inside. I know that declare it as final will solve the issue but I read that Java 8 should handle it automaticlly, I'm using Intellij with Java 8 and it still does not compile. Is there any other way to fix it without declare it as final? thanks. 回答1: I know that declare it as final will solve the issue but I read that Java 8 should handle it automatically. Java 8 will handle it if the variable is effectively

Reference outside the sealed class in Kotlin?

不问归期 提交于 2019-12-31 02:45:15
问题 I'm trying to create a class that uses its own state to operate on the state of an external object that it holds a reference to. The external object can be of class A or B, which are similar, but not controlled by the author. So a sealed class is created to access their common attributes, per this earlier answer from @SimY4. // *** DOES NOT COMPILE *** class A { // foreign class whose structure is not modifiable val prop get()= "some string made the Class-A way" } class B { // foreign class

Java Generics: Cannot create an array of a nested class

大憨熊 提交于 2019-12-30 19:54:54
问题 I'm trying to convert an AVLTree implementation into a heap style array and am having some problems with generics: public class MyAVLTree<K extends Comparable<? super K>, E> implements OrderedDictionary<K, E> { class AVLNode implements Locator<K, E>{ // ... } // .... public Locator<K,E> [] toBSTArray() { AVLNode[] bArray = new AVLNode[size]; makeArray(root, 0, bArray); // recursion return bArray; } } At the line AVLNode[] bArray = new AVLNode[size]; I get the following error: "Cannot create a

Java Generics: Cannot create an array of a nested class

浪尽此生 提交于 2019-12-30 19:53:11
问题 I'm trying to convert an AVLTree implementation into a heap style array and am having some problems with generics: public class MyAVLTree<K extends Comparable<? super K>, E> implements OrderedDictionary<K, E> { class AVLNode implements Locator<K, E>{ // ... } // .... public Locator<K,E> [] toBSTArray() { AVLNode[] bArray = new AVLNode[size]; makeArray(root, 0, bArray); // recursion return bArray; } } At the line AVLNode[] bArray = new AVLNode[size]; I get the following error: "Cannot create a

Why can't a class extend a static nested class occurring within it?

只愿长相守 提交于 2019-12-30 08:10:34
问题 This class: public class OuterChild extends OuterChild.InnerParent { public static class InnerParent { } } Fails to compile: $ javac OuterChild.java OuterChild.java:1: error: cyclic inheritance involving OuterChild public class OuterChild extends OuterChild.InnerParent { ^ 1 error because OuterChild would "depend on" itself, because (per §8.1.4 "Superclasses and Subclasses" of The Java Language Specification, Java SE 8 Edition) a class directly depends on any type that "is mentioned in [its]

How does Java inheritance work when inner classes are involved

流过昼夜 提交于 2019-12-30 05:58:10
问题 I am having trouble understanding how inheritance works in Java when inner classes are present. I'm currently working on something where a child class needs to slightly change the functionality of the inner class of it's parent. I've come up with an simpler, analagous example below. I expected this code to print "I am a ChildClass.InnerClass" but instead it prints "I am a ParentClass.InnerClass". Why is this? Also, if I change the obj object in main to be of type ChildClass then the output

Java inner class visibility puzzle

馋奶兔 提交于 2019-12-30 04:20:07
问题 Consider the following case: public class A { public A() { b = new B(); } B b; private class B { } } From a warning in Eclipse I quote that: the java complier emulates the constructor A.B() by a synthetic accessor method. I suppose the compiler now goes ahead and creates an extra "under water" constructor for B. I feel this is rather strange: why would class B not be visible as a.k.o. field in A? And: does it mean that class B is no longer private at run time? And: why behaves the protected

Access methods within local inner classes in Java

久未见 提交于 2019-12-30 03:25:07
问题 Is there any way to access the methods of local inner classes in Java. Following code is the sample code that I tried before. According to that what is the mechanism to access the mInner() method? class Outer{ int a=100; Object mOuter(){ class Inner{ void mInner(){ int y=200; System.out.println("mInner.."); System.out.println("y : "+y); } } Inner iob=new Inner(); return iob; } } class Demo{ public static void main(String args[]){ Outer t=new Outer(); Object ob=t.mOuter(); ob.mInner(); //

How to load nested classes in Java?

泪湿孤枕 提交于 2019-12-30 02:53:04
问题 I have the following java code: public class CheckInnerStatic { private static class Test { static { System.out.println("Static block initialized"); } public Test () { System.out.println("Constructor called"); } } public static void main (String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException { System.out.println("Inside main"); Class.forName("Test"); // Doesn't work, gives ClassNotFoundException //Test test = new Test(); // Works fine } } Why doesn't the