inner-classes

Implementing inner traits in Scala like we do with inner interfaces in Java

余生长醉 提交于 2019-11-30 18:55:31
This code in Java compiles without errors: interface T { interface Q { } } class C implements T.Q { } whereas this code in Scala does not: trait T { trait Q { } } class C extends T.Q { } What is the correct translation (if it exists) of the Java code listing into Scala? Theoretical explanations about language design are welcome. pagoda_5b The inner type Q is defined only for specific instance implementation of the T trait. Since scala has path-dependent types, each instance of T will have his own subtrait Q . scala> trait T { | trait Q | } defined trait T scala> class C extends T { | def getQ:

How does Java inheritance work when inner classes are involved

﹥>﹥吖頭↗ 提交于 2019-11-30 18:15:38
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 changes to "I am a ChildClass.InnerClass". Why is this? In general, what is the recommended way of

Java stopped erroring on non-final variables in inner classes (java 8) [duplicate]

扶醉桌前 提交于 2019-11-30 16:25:56
问题 This question already has answers here : Difference between final and effectively final (13 answers) Closed 4 years ago . Java 7 was saying "Cannot refer to the non-final local variable message defined in an enclosing scope" on following code: public class Runner { public static void main(String[] args) { String message = "Hello world"; new Runnable() { @Override public void run() { System.out.println(message); } }.run(); } } Java 8 does not. Suspect this is about adding functional

Java final static declarations in method local classes

人盡茶涼 提交于 2019-11-30 14:41:58
问题 When declaring a local inner class inside a method, why is it legal to include final static Strings or ints but not legal to include other objects? For instance: class Outer { void aMethod() { class Inner { final static String name = "compiles"; final static int ctr = 10; // compiles final static Integer intThree = Integer.valueOf(3); // does not compile! final static obj objConst = new Object(); // does not compile! } Inner inner = new Inner(); } } When I compile this, I get the following:

Are Inner Classes lightweight?

為{幸葍}努か 提交于 2019-11-30 13:50:55
Are inner classes more lightweight than normal classes, or in the end java compiles inner classes just like normal classes? I know classes in java are not all very lightweight themselves, and they occupy part of the permgen memory, so I'd like to know if it's best to use closure-like functions as inner classes, or if standard classes would also do fine? Inner classes and anonymous inner classes both compile down to .class files. For example: class Outer { class Inner { } Object function() { return new Object() { }; } } Will generate three .class files, Outer.class , Outer$Inner.class , and

Static inner classes in scala

巧了我就是萌 提交于 2019-11-30 12:42:16
问题 What is the analog in Scala of doing this in Java: public class Outer { private Inner inner; public static class Inner { } public Inner getInner() { return inner; } } I specifically want my inner class to not have to have a fully qualified name - i.e. I want Trade.Type , not TradeType . So in Scala I imagined it might be something like: class Outer(val inner: Inner) { object Inner } But this doesn't seem to work: my scala Inner just doesn't seem to be visible from outside the Outer class. One

Java final static declarations in method local classes

陌路散爱 提交于 2019-11-30 11:38:37
When declaring a local inner class inside a method, why is it legal to include final static Strings or ints but not legal to include other objects? For instance: class Outer { void aMethod() { class Inner { final static String name = "compiles"; final static int ctr = 10; // compiles final static Integer intThree = Integer.valueOf(3); // does not compile! final static obj objConst = new Object(); // does not compile! } Inner inner = new Inner(); } } When I compile this, I get the following: InnerExample.java:6: inner classes cannot have static declarations final static Integer outer = Integer

Access methods within local inner classes in Java

人走茶凉 提交于 2019-11-30 09:49:12
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(); // ?need a solution.. } } As ILikeTau's comment says, you can't access a class that you define in a method.

How many times can classes be nested within a class?

你说的曾经没有我的故事 提交于 2019-11-30 08:59:45
I came across this questions on one of the online Java Tests. The options were 4,5,8 and any number of times. I have only used one inner class, but have never tried multiple ones. I was wondering if anyone knows the answer. It's a completely irrelevant question and I hope they weren't using the results for anything important. I guess the answer they were looking for was 'any number of times' but in practice there will be a limit in any given implementation of Java. If it's not defined directly it will be determined by something like the maximum file size, or some other (possibly undocumented)

Can't access protected inner class while inheriting

放肆的年华 提交于 2019-11-30 08:54:05
Reading through "Thinking in Java" i stuck in ex:6 of Inner Classes chapter. Exercise 6: (2) Create an interface with at least one method, in its own package. Create a class in a separate package. Add a protected inner class that implements the interface. In a third package, inherit from your class and, inside a method, return an object of the protected inner class, upcasting to the interface during the return. This is my code: IOne.java interface package intfpack; public interface IOne{ void f(); } COne.java Class with protected inner class that implements the interface package classpack;