inner-classes

The final local variable cannot be assigned, since it is defined in an enclosing type

时光毁灭记忆、已成空白 提交于 2019-12-03 02:57:40
ratingS = new JSlider(1, 5, 3); ratingS.setMajorTickSpacing(1); ratingS.setPaintLabels(true); int vote; class SliderMoved implements ChangeListener { public void stateChanged(ChangeEvent e) { vote = ratingS.getValue(); } } ratingS.addChangeListener(new SliderMoved()); If i write the above code Eclipse tells me this: Cannot refer to a non-final variable vote inside an inner class defined in a different method But if i add final before int vote it gives me this error: The final local variable vote cannot be assigned, since it is defined in an enclosing type So, how to solve? Well, the standard

Memory leakage in event listener

主宰稳场 提交于 2019-12-03 02:42:47
问题 I have gone through the article http://android-developers.blogspot.com/2009/01/avoiding-memory-leaks.html. In this article it is suggested to use a static inner class with a WeakReference . Many inner classes are used for event listeners. Can those inner class also cause memory leaks? Should those inner class be static? 回答1: Can those inner class also cause memory leakage? Possibly. It depends on what those listeners are registered upon. For example, a well-written OnClickListener for a

Constructor reference for inner class fails with VerifyError at runtime

旧街凉风 提交于 2019-12-03 01:54:20
I am creating a supplier for an inner class constructor using the lambda ctx -> new SpectatorSwitcher(ctx) . IntelliJ suggested that I change it to SpectatorSwitcher::new instead. SpectatorSwitcher is a non-static inner class of the class I'm working in. The suggested code compiles fine (using maven) but I get the following VerifyError on execution: Exception in thread "main" java.lang.VerifyError: Bad type on operand stack Exception Details: Location: Test.lambda$runTest$8(LTest$Worker;)V @2: invokedynamic Reason: Type 'Test$Worker' (current frame, stack[1]) is not assignable to 'Test'

Why use method local abstract inner classes

左心房为你撑大大i 提交于 2019-12-03 01:22:06
One of the legal modifiers you can use with method local inner classes is abstract. For example: public class Outer { public void method(){ abstract class Inner{ } } } Is there any situation where you would actually use this? You have to know this for the SCJP exam. The are some invalid assumptions in the original question. That something is legal/valid Java doesn't mean that it is something that you need to use, or need to know. I can't recall that the SCJP contains odd corner case questions. I tried to come up with a case where I would have used an abstract class declared in a method, but

Access outer class “super” from inner class in Java

对着背影说爱祢 提交于 2019-12-02 23:33:25
How can I access outer class' super from an inner class? I'm overriding a method to make it run on a different thread. From an inline Thread, I need to call the original method but of course just calling method() would turn into an infinite recursion. Specifically, I'm extending BufferedReader: public WaitingBufferedReader(InputStreamReader in, long waitingTime) { [..] @Override public String readLine() { Thread t= new Thread(){ public void run() { try { setMessage(WaitingBufferedReader.super.readLine()); } catch (IOException ex) { } } }; t.start(); [..] } } This somewhere gives me a

how to restrict setting a property of innerclass just from the outer class in c#

旧时模样 提交于 2019-12-02 23:00:27
问题 I have the nub of the code like this: public class OuterClass { public static InnerClass GetInnerClass() { return new InnerClass() { MyProperty = 1 }; } public class InnerClass { public int MyProperty { get; set; } } } what is the solution to property named MyProperty just be settable from the InnerClass and the OuterClass , and out of these scopes, MyProperty just be readonly 回答1: There is no protection level for that. internal is the tightest you can use, which is limited to files in the

creating inner class objects with reflection

帅比萌擦擦* 提交于 2019-12-02 19:11:44
问题 How do you create an inner class object with reflection? Both Inner and Outer classes have default constructors that take no parameters Outer class { Inner class{ } public void createO() { Outer.Inner ob = new Inner ();//that works Inner.class.newInstance(); //<--why does this not compile? } } 回答1: "If the constructor's declaring class is an inner class in a non-static context, the first argument to the constructor needs to be the enclosing instance; see section 15.9.3 of The Java™ Language

Java OOP basics

纵饮孤独 提交于 2019-12-02 17:37:43
问题 I have a problem that keeps stalling me from advancing further, this error is not logical at all in my opinion , I am learning from a book and the code is from there. This is the code : package test_area; public class clzzz { class SimpleCircle{ double radius; SimpleCircle() { radius = 1; } SimpleCircle(double newRadius) { radius = newRadius; } double getArea() { return radius*radius*Math.PI; } double getPerimeter() { return 2*radius*Math.PI; } void setRadius(double newRadius) { radius =

Custom Enum in Java

☆樱花仙子☆ 提交于 2019-12-02 16:38:05
问题 I need to implement custom Enum. It should some class which implements Enum methods like valueOf, values and ordinal without directly extending java.lang.Enum. I have been thinking about creating it in such a way where instead of the String should be some generic type T: public class CustomEnum { private static int size; private static final InnerEnum enumValues; public CustomEnum(){ } public String valueOf(String innerEnum, String name){ if(name.equals(innerEnum)){ return innerEnum; } return

Memory leakage in event listener

不问归期 提交于 2019-12-02 16:17:04
I have gone through the article http://android-developers.blogspot.com/2009/01/avoiding-memory-leaks.html . In this article it is suggested to use a static inner class with a WeakReference . Many inner classes are used for event listeners. Can those inner class also cause memory leaks? Should those inner class be static? Can those inner class also cause memory leakage? Possibly. It depends on what those listeners are registered upon. For example, a well-written OnClickListener for a Button should not result in a memory leak, because even though the OnClickListener may be an inner class and