inner-classes

Inner Class. What is its purpose?

假装没事ソ 提交于 2019-12-05 10:36:31
Can someone tell me what the purpose of having inner classes? I can think of a few but may be they are not good reasons for using inner classes. My reasoning is that inner class is helpful when you want to use a class that no other classes can use. What else? Inner classes can be used to simulate closures: http://en.wikipedia.org/wiki/Closure_(computer_science)#Java When I was learning Java we used inner classes for GUI event handling classes. It is sort of a "one time use" class that need not be available to other classes, and only is relevant to the class in which it resides. I use inner

Java - local class and generics, why compiler warning?

ぃ、小莉子 提交于 2019-12-05 09:42:35
Named local classes are very rarely used, usually local classes are anonymous. Does anybody know why the code below generates a compiler warning? public class Stuff<E> { Iterator<E> foo() { class InIterator implements Iterator<E> { @Override public boolean hasNext() { return false; } @Override public E next() { return null; } @Override public void remove() { } } return new InIterator(); } } The warning is in new InIterator() and it says [unchecked] unchecked conversion found : InIterator required: java.util.Iterator<E> If the class, unchanged, is made anonymous, or if it is made a member, the

python: determine if a class is nested

孤人 提交于 2019-12-05 08:15:54
Suppose you have a python method that gets a type as parameter; is it possible to determine if the given type is a nested class? E.g. in this example: def show_type_info(t): print t.__name__ # print outer class name (if any) ... class SomeClass: pass class OuterClass: class InnerClass: pass show_type_info(SomeClass) show_type_info(OuterClass.InnerClass) I would like the call to show_type_info(OuterClass.InnerClass) to show also that InnerClass is defined inside OuterClass. AFAIK, given a class and no other information, you can't tell whether or not it's a nested class. However, see here for

How to ignore inner/nested classes with JaCoCo?

天大地大妈咪最大 提交于 2019-12-05 08:02:43
I'm trying to ignore some generated classes, and the classes get ignored fine. But if those classes have inner classes, those classes still get included, despite the parent class being excluded. This is my configuration: <plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.7.9</version> <executions> <execution> <goals> <goal>prepare-agent</goal> </goals> </execution> <execution> <id>report</id> <phase>prepare-package</phase> <goals> <goal>report</goal> </goals> <configuration> <excludes> <exclude>**/*DB.*</exclude> <exclude>**/*DTO.*</exclude> <

Java compiler fails to recognise static inner class

≡放荡痞女 提交于 2019-12-05 07:56:45
This is quite a complicated error, so please bear with me. I am seeing a weird error when trying to compile some Java code. The compiler fails to recognise a static inner class . Let us say that I am working on a class MyClass . The static inner class which I need to use has an FQN of x.y.z.Parent.DesiredClass . This inner class is explicitly imported using its FQN. The parent is also imported using its FQN. Now there exists another package with (another, different FQN) which has a class DesiredClass . This other DesiredClass is on the classpath, but it is not being explicitly imported. Before

Instantiating an inner class (Preference) in xml file

时光总嘲笑我的痴心妄想 提交于 2019-12-05 02:13:05
When you want to access a custom view in some layout.xml file, you have two options: The view is in it's own class. Then you do <package.name.MyView android:layout_width= ... /> The view is an inner class: <view class="package.name.OuterClass$MyView" android:layout_width= ... /> Now I want to do the same thing inside a <PreferenceScreen> . The first way works well, but I would like to put all the custom Preference classes together in my PreferenceActivity. I tried <Preference class="package.name.OuterClass$MyPreference" ... /> (also with '.' instead of '$') as well as <package.name.OuterClass

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

倖福魔咒の 提交于 2019-12-05 01:45:40
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 , k == 5 , so clearly we've made a reference, order matters, and no warning or compilation error. Is

Java Inner Class Access and Best Practices

倖福魔咒の 提交于 2019-12-05 01:39:45
I know that an inner class has access to everything in the outer class (because it's a member of that class) but what about the other way around? Does the outer class have access to private variables and methods within an inner class? I've seen articles mentioning that inner classes should be private so that they are accessible only to the outer class. What does that do to the accessibility of that inner class? What is best practices in dealing with access levels when it comes to your inner classes? I'm assuming more encapsulation the better but does that come at the expense of accessibility?

Android Weak Reference of Inner Class

你。 提交于 2019-12-05 01:00:48
问题 I have gone through the article http://developer.android.com/resources/articles/avoiding-memory-leaks.html . In this article it is suggested to use static inner class with Weak Reference . public class GalleryVideo extends Activity { private int AUDIO_NO = 1; ........................... ................ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); gallery = (Gallery) findViewById(R.id.examplegallery); gallery.setAdapter(new AddImgAdp(this));

How can I pass a non final variable to an anonymous inner class?

浪尽此生 提交于 2019-12-05 00:25:54
问题 I have these lines of code. I know you can not pass a non final variable to an inner class but I need to pass the variable i to the anonymous inner class to be used as a seatingID. Can you suggest ways of doing that ? JButton [] seats = new JButton [40]; //creating a pointer to the buttonsArray for (int i = 0; i < 40; i++) { seats[i] = new JButton();//creating the buttons seats[i].setPreferredSize(new Dimension(50,25));//button width panel4seating.add(seats[i]);//adding the buttons to the