inner-classes

Creating instances of public inner classes of generic classes

穿精又带淫゛_ 提交于 2019-12-10 13:30:49
问题 So I have something like the following: public class Enclosing<T extends Comparable<T>> { // non-relevant code snipped public class Inner { private T value; public Inner(T t) { value = t; } } } Everything compiles and the world is happy. However, whenever I try to create an instance of Enclosing.Inner as follows, I can't: new Enclosing<Integer>.Inner(5); The following error happens: Cannot allocate the member type Enclosing<Integer>.Inner using a parameterized compound name; use its simple

Firing events from inner class which extends SwingWorker

别等时光非礼了梦想. 提交于 2019-12-10 11:44:58
问题 I am trying to fire events from inner class, but it is not working. This is my code: ABSTRACT MODEL: public abstract class AbstractModel { public PropertyChangeSupport propertyChangeSupport; public AbstractModel() { propertyChangeSupport = new PropertyChangeSupport(this); } public void addPropertyChangeListener(PropertyChangeListener listener) { propertyChangeSupport.addPropertyChangeListener(listener); } public void removePropertyChangeListener(PropertyChangeListener listener) {

Is there a way with Mockito to run a stubbed method of an spied object from an inner class?

爱⌒轻易说出口 提交于 2019-12-10 11:18:01
问题 I'm writing junit tests using Mockito for a code that has been implemented by someone else. Simplifying: There is an Outer class and an Inner class The Outer class keeps an instance of the Inner class. The Inner class uses a method of the Outer class. The simplified code could looks like this: public class Outer { private Inner inner; public Outer(){ inner=new Inner(); } public Inner getInner(){ return inner; } public String getOuterName(){ return "outer"; } public String getOuterNiceName(){

“Default member initializer needed within definition of enclosing class outside of member functions” - is my code ill-formed?

时光怂恿深爱的人放手 提交于 2019-12-10 03:09:11
问题 struct foo { struct bar { ~bar() {} // no error w/o this line }; bar *data = nullptr; // no error w/o this line foo() noexcept = default; // no error w/o this line }; Yes, I know, there is another question with exactly the same title, but a somewhat different problem (involving a noexcept operator and no nested type). The solution suggested there (replacing the constructor of foo with foo() noexcept {} ) changes the semantics and it not necessary here: here we have a better answer (hence the

How to use java proxy in scala

我与影子孤独终老i 提交于 2019-12-09 16:44:55
问题 I have an interface as Iface that has two methods written in java. That interface is an inner interface of Zzz class. I have written the invocation handler in scala. Then I tried to create a new proxy instance in scala like below. val handler = new ProxyInvocationHandler // this handler implements //InvocationHandler interface val impl = Proxy.newProxyInstance( Class.forName(classOf[Iface].getName).getClassLoader(), Class.forName(classOf[Iface].getName).getClasses, handler ).asInstanceOf

Inner Classes vs. Subclasses in Java

房东的猫 提交于 2019-12-09 12:22:57
问题 Is there a situation where it is more beneficial to use an inner class over a subclass in Java (or vice-versa)? Based on my current understanding, inner classes have access to the fields and methods of the outer class. How is this any different from using inheritance? Subclasses generally have access to all the fields/methods labeled public and protected. Fields labeled private in the parent class can be accessed in the subclass using a getter method. Based off what I've seen thus far, when

inner class non-final variable java

此生再无相见时 提交于 2019-12-09 10:32:25
问题 I needed to change variables inside an inner class and I got the infamous "Cannot refer to a non-final variable inside an inner class defined in a different method" error. void onStart(){ bt.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { int q = i; } }); } I quickly made a class that held all of the things I wanted to change and made a final version of the class outside the inner class class temp{ int q; } void onStart(){ final temp x = new temp(); bt

No enclosing instance of type MySuperClass<B> is available due to some intermediate constructor

▼魔方 西西 提交于 2019-12-09 09:23:47
问题 I was trying to use an inner class of the super type, which was using generics. And got that strange error above. class MySuperClass<B> { class InnerClass { } MySuperClass(InnerClass... c) { } } In the sub class I tried to instantiate it: class MySubClass extends MySuperClass<String> { MySubClass() { super(new InnerClass(), new InnerClass()); } } The compiler confused me with No enclosing instance of type MySuperClass<B> is available due to some intermediate constructor Why? 回答1: Heh, and

Java: How to check if an object is an instance of a non-static inner class, regardless of the outer object?

孤街醉人 提交于 2019-12-09 05:08:55
问题 If I have an inner class e.g. class Outer{ class Inner{} } Is there any way to check if an arbitrary Object is an instance of any Inner , regardless of its outer object? instanceof gives false when the objects are not Inner s from the same Outer . I know a workaround is just to make Inner a static class, but I'm wondering if what I'm asking is possible. Example: class Outer{ Inner inner = new Inner(); class Inner{} public boolean isInner(Object o){ return o instanceof Inner; } } Outer outer1

Why should I use nested classes? [closed]

只谈情不闲聊 提交于 2019-12-09 02:20:15
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed last year . When is it feasible to nest classes? The most common advantage of it that I see is "shared scope" (use of variables across classes). Is this less attractive/less a best practice than just putting the nested class in it's own file, and passing the arguments through the