inner-classes

Java inner class and static nested class

What is the main difference between a inner class and a static nested class in Java? Does design /implementation play a role in choosing any of these? ...

Can inner classes access private variables?

class Outer { class Inner { public: Inner() {} void func() ; }; private: static const char* const MYCONST; int var; }; void Outer::Inner::func() { var = 1; } const char* const Outer::MYCONST = "myconst"; This errors out when I compile with class Oute...

Practical side of the ability to define a class within an interface in Java?

What would be the practical side of the ability to define a class within an interface in Java: interface IFoo { class Bar { void foobar () { System.out.println("foobaring..."); } } } ...

protected/public Inner Classes

Can someone please explain to me what is the difference between protected / public Inner classes? I know that public inner classes are to avoid as much as possible (like explained in this article). But from what I can tell, there is no difference between using protected or public modifiers. Take a look at this example: public class F...

Strange syntax for instantiating an inner class

I didn't imagine that I would encounter radically new syntax in Java anymore at this stage, but lo and behold, I just encountered something: The exact context and what the code below should do is pretty irrelevant - it's there just to give some kind of context. I'm trying to synthetically create an event in IT Mill Toolkit, so I wrote ...

python: determine if a class is nested

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(Some...

Is there a way for anonymous inner classes in Java to "lose" their scope?

When I pass an anonymous inner class into a function, I can refer to variables in the current scope from within a method of that class, like so: class Caller { private Object callerPrivate; // ... public void someMethod() { final String callerLocal = "Eyes only"; ISomeInterface anon = new ISomeInterface() ...

Create a regular class or an Inner class for a Parameter Object?

Instead of passing many arguments to a method, I was encapsulating it into an argument object. note: simplied for demo For such a case, what would be a better practice? • Create a class and name it as InventorySaveArgs? -- or -- • Create a nested class and name it as SaveArgs? And would you also explain why one would choose...

Instantiating an inner class

I have a utility method and when irrelevant logic is removed from it, the simplified method would look like this: public static <A extends Foo> List<A> getFooList(Class<A> clazz) { List<A> returnValue = new ArrayList<A>(); for(int i=0; i < 5; i++) { A object = clazz.newInstance(); returnValue.add(object); } re...

Is it possible to make anonymous inner classes in Java static?

In Java, inner classes can be either static or not. If they are static, they do not contain a reference to the pointer of the containing instance (they are also not called inner classes anymore, they are called nested classes). Forgetting to make an inner class static when it does not need that reference can lead to problems with garbage...

Java - local class and generics, why compiler warning?

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 ...

Using Inner classes in C#

What are the best practices regarding the use and structure of inner classes in C#. For instance if I have a very large base class and two large inner classes should I split them up into separate (partial class) codefiles or leave them as one very large unwieldy codefile? Also is it bad practice to have an abstract class, with a public...

Are inner classes commonly used in Java? Are they "bad"?

Are inner classes commonly used in Java? Are these the same as nested classes? Or have these been replaced in Java by something better? I have a book on version 5 and it has an example using an inner class, but I thought I read somewere that inner classes were "bad." I have no idea and was hoping for thoughts on it. Thank you. ...

Static inner classes in scala

What is the analog in Scala of doing this in Java: public class Outer { private Inner inner; public static class Inner { } public Inner getInner() { return inner; } } I specifically want my inner class to not have to have a fully qualified name - i.e. I want Trade.Type, not TradeType. So in Scala I imagined it might be somet...

Java inner class visibility puzzle

Consider the following case: public class A { public A() { b = new B(); } B b; private class B { } } From a warning in Eclipse I quote that: the java complier emulates the constructor A.B() by a synthetic accessor method. I suppose the compiler now goes ahead and creates an extra "under water" constructor for B. I feel this is ...

How to use the type parameter of a generic parent inside of local classes?

Why can't I reference the type parameter of a generic parent class inside of contained local classes? public class IsGeneric<T> { public void doSomething(T arg) { class A { T x; } A foo = new A(); foo.x = arg; T bar = foo.x; // error: found java.lang.Object, required T } } ...

Accessing private class in operator<< in namespace

Hi, I have a class CFoo with a private inner class CBar. I want to implement a stream ouput operator for CFoo, which in turn uses a stream output for CBar in it's implementation. I can get this working when CFoo is in the common namespace, but when i place it in a new namespace (namespace foobar), the operator can no longer access the pr...

Java: Static vs non static inner class

What is the difference between static and non static inner class? ...

AspectJ Inner-Class Join points

Hi everyone, I wonder is there a way to reach the code using aspect in "//do something" part? Thanks in advance. Turan. public class Test { private class InnerTest { public InnerTest() { JButton j = new JButton("button"); j.addActionListener(new ActionListener() { public void action...

How can I use an inner class instance from another inner class method ?

public class Ex7 { private int fld; private void meth(int val) { fld = val; System.out.println(" meth() -> fld = " + fld); } public class Ex7Inner1 { void operateOnFld() { fld = 12; } void operateOnMeth() { meth(10); } public void bar() { ...