inner-classes

In Java, how can I access static method parameters inside a new listener block?

青春壹個敷衍的年華 提交于 2019-12-02 06:02:42
问题 I have a static method that accepts a couple of parameters. Inside the method I am creating a new object and attaching a new listener to it. The problem is that the listener block needs access to the outer static method variables, but I don't know how to reference them. I know how to make this happen with a non static method, but not with a static one. Here is the code: v.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event

How can I instantiate a member class through reflection on Android

与世无争的帅哥 提交于 2019-12-02 03:56:47
I have a saving/loading framework that is supposed to save arbitrary object graphs. This includes instances of non-static nested classes. Nested classes require are owned by the instance of their parent class that created them, and the nested class knows what instance it belongs to using a synthetic field. As a simple example, I present this class: public class Foo implements Savable { private class Bar implements Savable { public void saveState(Saver saver) { saver.putInt(3); } } private Bar myBar = new Bar(); public void saveState(Saver saver) { saver.putSavable(myBar); } } On the "standard"

In Java, how can I access static method parameters inside a new listener block?

穿精又带淫゛_ 提交于 2019-12-02 01:29:51
I have a static method that accepts a couple of parameters. Inside the method I am creating a new object and attaching a new listener to it. The problem is that the listener block needs access to the outer static method variables, but I don't know how to reference them. I know how to make this happen with a non static method, but not with a static one. Here is the code: v.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_UP: ((Activity)*context*).startActivityForResult(*intent*, 0);

Reference outside the sealed class in Kotlin?

懵懂的女人 提交于 2019-12-02 01:17:35
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 whose structure is not modifiable val prop get()= "some string made the Class-B way" } data class

Access to private field of a super class

本小妞迷上赌 提交于 2019-12-01 22:05:30
As everyone knows, private fields are not inherited between classes. What intrigues me, is how it works for inner static classes. Consider the following code: public class Main { public static void main(String[] args) { new B(); } private static class A { private int a = 10; private void foo() { System.out.println("A.foo"); } } private static class B extends A { { // foo(); // compile-time error super.foo(); // ok // System.out.println(a); // compile-time error System.out.println(super.a); // ok } } } Can you please explain how it is possible to access private fields of other inner class? And

Access to private field of a super class

*爱你&永不变心* 提交于 2019-12-01 21:56:11
问题 As everyone knows, private fields are not inherited between classes. What intrigues me, is how it works for inner static classes. Consider the following code: public class Main { public static void main(String[] args) { new B(); } private static class A { private int a = 10; private void foo() { System.out.println("A.foo"); } } private static class B extends A { { // foo(); // compile-time error super.foo(); // ok // System.out.println(a); // compile-time error System.out.println(super.a); //

Tool to Delambdafy Java code from Java 8 to Java 7 syntax? [closed]

一世执手 提交于 2019-12-01 19:19:12
Does anyone know of any tool to convert Java 8 code (at the source level) that uses lambdas and method references into Java 7 code that uses anonymous inner classes? I know about Retrolambda , but that works at the bytecode level, not the source level. For now, I have a version working that works as an IntelliJ plugin. I extended the current IntelliJ code to convert all lambdas in a package at one go, instead of selecting each lambda individually and converting to anonymous inner class . The problem with this approach though is that it cannot work as a standalone tool, say a maven plugin as it

Error: field name cannot be declared static

五迷三道 提交于 2019-12-01 18:27:15
问题 public class Application { public static void main(String[] args) { final class Constants { public static String name = "globe"; } Thread thread = new Thread(new Runnable() { @Override public void run() { System.out.println(Constants.name); } }); thread.start(); } } Compilation Error: The field name cannot be declared static in a non-static inner type, unless initialized with a constant expression Solution to this? 回答1: Java does not let you define non-final static fields inside function

Error: field name cannot be declared static

允我心安 提交于 2019-12-01 18:10:25
public class Application { public static void main(String[] args) { final class Constants { public static String name = "globe"; } Thread thread = new Thread(new Runnable() { @Override public void run() { System.out.println(Constants.name); } }); thread.start(); } } Compilation Error: The field name cannot be declared static in a non-static inner type, unless initialized with a constant expression Solution to this? Java does not let you define non-final static fields inside function-local inner classes. Only top-level classes and static nested classes are allowed to have non-final static

Defining inner class outside java file

本小妞迷上赌 提交于 2019-12-01 17:56:58
I want to create a class, ClassB , as inner class of ClassA , but I want to write down outside ClassA.java file. How can I do this? It will be a lot of inner class, and ClassA.java file will be enormous . UPDATE What I really want to do is define ten classes that they will be only accessible by one class. All of them are defined inside the same package. Thanks. Put all your classes in a package and define the classes to be package private. package com.example.here class Hello{ //... } Notice the absence of the keyword public ? You will only be able to create an instance of the class Hello if