inner-classes

The difference of static and non-static inner classes?

半城伤御伤魂 提交于 2019-12-11 00:55:22
问题 I was reading Effective Java 2 - Item 22 and it says in the title: "Favor static member classes over non-static" but at the end of the chapter Implementations of the collection interfaces, such as Set and List, typically use nonstatic member classes to implement their iterators: // Typical use of a nonstatic member class public class MySet<E> extends AbstractSet<E> { ... // Bulk of the class omitted public Iterator<E> iterator() { return new MyIterator(); } private class MyIterator implements

Method Local Inner Class

核能气质少年 提交于 2019-12-10 23:58:59
问题 public class Test { public static void main(String[] args) { } } class Outer { void aMethod() { class MethodLocalInner { void bMethod() { System.out.println("Inside method-local bMethod"); } } } } Can someone tell me how to print the message from bMethod ? 回答1: You can only instantiate MethodLocalInner within aMethod . So do void aMethod() { class MethodLocalInner { void bMethod() { System.out.println("Inside method-local bMethod"); } } MethodLocalInner foo = new MethodLocalInner(); //

How do I call an outer class method in my inner class

℡╲_俬逩灬. 提交于 2019-12-10 22:13:12
问题 So I was having a bug where my code was crashing because I am calling methods in my outer class in onPostExecute . So this is my simple class: public class UsersList extends Fragment{ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { ... } public void callMe(){ Toast.makeText(getContext(), "I work", Toast.LENGTH_LONG).show(); } public class UserRecommend extends AsyncTask<String, String, String> { @Override protected String

How to declare template member function of template class X as friend of nested class X::Y

余生颓废 提交于 2019-12-10 18:39:41
问题 I have a template class. It has a template function. Both take different template parameters. There is an internal class that needs to make a friend of the enclosing class's template function. Compiler errors abound. The following toy example shows my issue. First, the following of course compiles (VS 2017): template <typename T> class Class1 { public: Class1() = default; ~Class1() = default; template <typename U> void Func(U& x) {}; }; class Class2 { public: Class2() = default; ~Class2() =

Mockito java.lang.Exception: Class should be public when I use inner classes in tests

流过昼夜 提交于 2019-12-10 16:47:25
问题 I have following test: @RunWith(Enclosed.class) public class ProductTest { @RunWith(MockitoJUnitRunner.class) @Ignore public static abstract class Base1 { @Before public void setUpBase() {...} } public static class Test1 extends Base1{ @Test public void foo(){...} } } public static class Test2 extends Base1{ @Test public void bar(){...} } } } To avoid @Ignore I refactored class like this: @RunWith(Enclosed.class) public class ProductTest { @RunWith(MockitoJUnitRunner.class) public static

Inner generic type same as outer - compiler warning

心已入冬 提交于 2019-12-10 16:39:15
问题 So I have a tree class: public class Tree<T> : IEnumerable where T : IComparable<T> { private Node<T> root = null; ... private class Node<T> : IEnumerable<T> where T : IComparable<T> { private Node<T> left, right; ... } } It works fine, but I get the compiler warning: Type parameter 'T' has the same name as the type parameter from outer type 'Tree<T>' Well, of course it's the same name, they should be the same type. (In fact, since the Node class is private and thus can never be accessed

Java singleton inner class

怎甘沉沦 提交于 2019-12-10 15:24:44
问题 I know the concept of singleton in Java. I'm having problems with creating singleton as inner class in Java. Problem occurs at holder's public class NormalClass { private class Singleton { private static Singleton instance = null; private Singleton() { } private static class SingletonHolder { private static Singleton sessionData = new Singleton(); } public static Singleton getInstance() { return NormalClass.Singleton.SingletonHolder.sessionData; } } public void method1() { Singleton

Scala: Extending inner class, without reference to outer class

我怕爱的太早我们不能终老 提交于 2019-12-10 15:17:16
问题 I can extend an inner class/trait inside the outer class or inside a class derived from the outer class. I can extend an inner class of a specific instance of an outer class as in: class Outer { class Inner{} } class OtherCl(val outer1: Outer) { class InnA extends outer1.Inner{} } Note: even this seems to compile fine producing very interesting possibilities: trait OuterA { trait InnerA } trait OuterB { trait InnerB } class class2(val outerA1: OuterA, val outerB1: OuterB) { class Inner2

Access to a Java static inner class with Clojure

亡梦爱人 提交于 2019-12-10 14:31:46
问题 I'm trying access to a static inner class method, but I can't find the right way. I need to write this java code in Clojure: SessionProperties sessionProperties = SessionProperties.Builder().mediaMode(MediaMode.ROUTED).build(); My code is: (:import [com.opentok OpenTok MediaMode SessionProperties SessionProperties$Builder])) (def sessionProperties (.build (.mediaMode SessionProperties$Builder MediaMode/ROUTED)) And this is the error: java.lang.IllegalArgumentException: No matching method

Defining an Inner class member function template with a (non type) enum argument

只愿长相守 提交于 2019-12-10 13:37:22
问题 I'm having difficulty defining and specializing a member function update() of an inner class Outer<T1>::Inner that is templated on a non-type (enum) argument. #include <cstdlib> template<typename T1> struct Outer { struct Inner { enum Type{ A , B , C }; template<Type T2> void update(); }; }; // Definition template<typename T1> template<Outer<T1>::Inner::Type T2> void Outer<T1>::Inner::update() { } // Specialization template<typename T1> template<Outer<T1>::Inner::A > void Outer<T1>::Inner: