inner-classes

Macros: knownDirectSubclasses broken with nested type?

Deadly 提交于 2020-01-04 06:27:22
问题 I have a macro which enumerates the direct sub types of a sealed trait: import scala.reflect.macros.Context import language.experimental.macros object Checker { def apply[A]: Unit = macro applyImpl[A] def applyImpl[A: c.WeakTypeTag](c: Context): c.Expr[Unit] = { val tpe = c.weakTypeOf[A].typeSymbol.asClass require (tpe.isSealed) tpe.typeSignature // SI-7046 require (tpe.knownDirectSubclasses.nonEmpty) import c.universe._ c.Expr[Unit](reify {} .tree) } } Then this works: sealed trait A case

Is there a need to declare access modifiers in a private inner class

泪湿孤枕 提交于 2020-01-04 01:55:28
问题 Let's say I have a class like this: public class OuterClass { //... private class InnerClass { private int x; // This variable makes sense public int y; // Is there any use for this? } } In the code above, since the inner class is private, only the outer class has access to all its variables, even the private ones. The inner class itself is not visible to any other class but the enclosing outer class. So even though variable y above is public, it cannot be accessed by any other class other

Inner Class has an implicit reference to the outer class and may can leak memory

不羁岁月 提交于 2020-01-03 13:41:13
问题 After learning about the inner class, I understand it has an implicit reference to the outer class. But my teacher told me that the best way is not use inner class, prefer to use static inner class. Because the inner class may leak memory. Can someone kindly explain about this? 回答1: In the answer to your comment (it would be unreadable if I posted it in the comments), where it belongs. Example of accesing inner class outside the outer. public class Dog { String name; } public class HugeKennel

Android inner classes memory leak and leak by context?

爱⌒轻易说出口 提交于 2020-01-02 10:04:16
问题 I am using Handler in my splash screen for delaying redirection to the next activity as follows.. @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.entrance); screenTimeOut(); } private void screenTimeOut() { /* New Handler to start the next screen * and close this Entrance after some seconds.*/ new Handler().postDelayed(new Runnable() { @Override public void run() { initTracker(); /* Create an Intent that will start the

Python - reference inner class from other inner class

南楼画角 提交于 2020-01-02 07:42:11
问题 I am trying to reference an inner class from another inner class. I have tried both : class Foo(object): class A(object): pass class B(object): other = A and class Foo(object): class A(object): pass class B(object): other = Foo.A with respective results: Traceback (most recent call last): File "python", line 1, in <module> File "python", line 6, in Foo File "python", line 7, in B NameError: name 'A' is not defined and Traceback (most recent call last): File "python", line 1, in <module> File

Java: Why no warning when referencing a field before it is defined?

孤街醉人 提交于 2020-01-02 01:09:36
问题 A static field cannot be referenced before it is defined or initialized: static Integer j = i; /* compile error */ static final Integer i = 5; However, when it is referenced from an instance initialization block (in an anonymous inner class), not even a warning is generated. See example: class StaticInitialization { static final Object o = new Object() {{ j = i; }}; static Integer j, k; static final Integer i = 5; static final Object o2 = new Object() {{ k = i; }}; } The result is: j == null

Anonymous Inner class

浪尽此生 提交于 2020-01-01 21:38:15
问题 class One { Two two() { return new Two() { Two(){} Two(String s) { System.out.println("s= "+s); } }; } } class Ajay { public static void main(String ...strings ){ One one=new One(); System.out.println(one.two()); } } The above sample code cannot be compiled.It says "Two cannot be resolved". What is the problem in this code?? 回答1: new Two() { Two(){} Two(String s) { System.out.println("s= "+s); } }; An anonymous inner class is called anonymous because it doesn't have its own name and has to be

Which part of JLS said anonymous classes cannot have public/protected/private member classes

大兔子大兔子 提交于 2020-01-01 08:29:57
问题 Consider this piece of code: public class TopLevelClass { Cloneable c = new Cloneable() { private int privateField; private void privateMethod() {}; }; } There is an anonymous class that has a private member field and a private member method. It has been successfully compiled. Then consider this one: public class TopLevelClass { Cloneable c = new Cloneable() { private class PrivateInnerClass {} }; } There is an anonymous class that has a private member class. However... javac said: error:

Using an inner class name and an object name same in Java

◇◆丶佛笑我妖孽 提交于 2020-01-01 04:24:06
问题 In the following code snippet, presumably it appears that it should issue some compilation error but it doesn't: class Outer { public static class Inner { static String obj = "Inner"; } static Optional Inner = new Optional(); //The (inner) class name and the object name are same. } class Optional { String obj = "Optional"; } public class Main { public static void main(String[] args) { System.out.println(Outer.Inner.obj); //Refers to the string inside the optional class } } The class Outer has

C++ - What's the point of nested classes?

让人想犯罪 __ 提交于 2020-01-01 01:13:49
问题 I'm studying a little of C++ and now I'm fighting against it's similitudes with Java. I know the purpose of inner classes in Java, but now I'm trying to use nested classes in C++, and I discover that private attributes of "container" class are not visibile by nested class , so why I should use them? Also, is there a way to make visibile those attributes? 回答1: I'm studying a little of C++ and now I'm fighting against it's similitudes with Java. First of all be aware that C++ nested classes are