I am confused on why should or when shall I write an inner class in java.
Say I encountered a method like execute in some ExecutorService class .parameter of execute is runnable type.Why shall I write inner class for parameter of this method.
Also ,How inheritance work on inner classes like My class has some inner class and how inheritance (i.e. overriding overloading works there).
Pointers\tutorials are welcome.
Thanks in advance
Gaurav
When we use inner classes, we reference their objects with OuterClassName.InnerClassName.
This is necessary because objects of any non-static inner class can only be created in association with an object of the outer class, outside of any non-static method within the outer class.
Take a look at Using inner and nested Java classes for more information and exmples.
Related
There are many reasons to choose one design over another, and there are really good answers to it found in here:
Java inner class and static nested class
I am curious if there are any performance or memory/storage difference when choosing static inner class via a regular class.
How does the difference looks like after compilation?
To be clear, This question is not about giving advice what to use in each case. I just want to learn how Java works here, behind the scene.
The static nested class is the simplest form: it works just like a top level class, the only difference is the scope in which it is declared. Nothing is hidden there.
The non-static nested class have a hidden reference to an instance of the enclosing class, that's why you can reference non-static fields of the enclosing class from inside the inner class.
When you define an anonymous class in a method, you will also have a copy of all the local variables declare in the method and referenced from within the anonymous class. If the method is non-static, you will also have the hidden reference to the enclosing class.
Reading chapter 20 of Ordesky's book on Scala, I found that inner classes are path dependant. Among other features, that implies that they can only be instantiated within the outer class or giving an outer class instance.
The question arises: I would like to implement an static inner class in Scala but the author suggest that is not possible.
I immediatelly thought of making the "inner class" (lets call it Inner) a member of Outer's companion object.
The accepted answer of this question seems to point towards that direction.
But that drives to a problem: Inner's type ins't Outer#Inner, I could try something like:
object Outer {
class Inner extends Outer#Inner { }
}
This doesn't work however. Do you know a work arround for this?
I have the hunch that it could be done with abstract types but I am not sure.
Note that making Inner an inner class of the companion objects is not exactly as having a non-path-dependant Inner class because of its type.
I immediatelly thought of making the "inner class" (lets call it Inner) a member of Outer's companion object.
Yes, that's the closest Scala equivalent.
But that drives to a problem: Inner's type ins't Outer#Inner
This isn't a problem, because Outer#Inner is equivalent to a non-static inner class of Outer in Java. I.e. it has a reference to an Outer object.
I would like to get a inner class which is not path dependant or, at least, to know if that is possible
If you want to create a non-companion inner class which can't be used path-dependently, it isn't possible. You are free to always write Outer#Inner instead of o.Inner in your code, of course.
I have code establishing a server connection upon the event that a user clicks a specific button. I created an inner class to listen for the action. Within the single method I have in the inner class, I also establish that server connection mentioned earlier.
My question is, can the Socket connection only be utilized from within the "inner" class? Or, can the outer class proceed with communication with said server?
I do, however, understand that the inner class has unrestricted access to the outer class(as if it were the outer class. My question is the other way around.
Create an instance like this and access what you want:
OuterClass.InnerClass innerObject = outerObject.new InnerClass();
All methods declared on the inner class are accessible ... whether they are declared as public or ... private.
If the inner methods are static then they can always be called by code in the outer class. You just need to qualify the method name with the inner class name.
Otherwise, the outer class code needs a reference for an instance of the inner class to call methods on it. (But that's normal.)
(If you were asking about whether an inner class could call methods on the outer class, it is a bit more complicated. Most of the above applies, but if the inner class is NOT static it can also call instance methods on its outer class via this.)
Yes ,you can achieve this look below sample code
currentDateMinutes=getDateAndTime();
System.out.println("DATE & TIME:"+new JobSchedulerUtil().new TaskScheduler(currentDateMinutes).timeNow());
Above code JobSchedulerUtil class is an outer class with having getDateAndTime() method and an inner class TaskScheduler with timeNow() method.
If I nest a class inside another, does the nested class automatically become a subclass of the parent class? Would the nested class have access to all the public methods, vairables of the parent class?
Oh sorry - the programming language i am referring mainly to is Java!
If I nest a class inside another, does the nested class automatically become a subclass of the parent class?
No
Would the nested class have access to all the public methods, vairables of the parent class?
If the inner class is not static, yes. It also has access to any other members, static or not, and public or not. If the nested class is declared static then there is no enclosing instance, so it would only have access to the static members of the outer ("parent") class.
Nested class is not subclass of parent class. If nested class is not static , it can access all methods and variables of the parent class. If nested class is static, then it can access only static fields and methods.
What language is this in reference to? For most languages, an inner class and a subclass are completely different things. An inner class is a "member" of the outer class, just as fields and methods are members of the outer class. And just like any (static) method of the outer class can access all (static) class members, usually inner classes have access, as members of the outer class, to all other members of the outer class. (This is definitely not true of all OO languages, however.)
(Java question)
If I reference a field in an inner class, does this cause a circular dependency between the enclosing class and the inner class?
How can I avoid this?
Here is an example:
public class Outer {
private Other o;
private Inner i;
public Outer() {
o = new Other();
i = new Inner() {
public void doSomething() {
o.foo();
}
};
}
}
Static vs instance class: If you declare the inner class as static then the instances of the inner class doesn't have any reference to the outer class. If it's not satic then your inner object efectivelly points to the outer object that created it (it has an implicit reference, in fact, if you use reflection over its constructors you'll see an extra parameter for receiving the outer instance).
Inner instance points outer instance: Circular reference is in case each instance points the other one. A lot of times you use inner classes for elegantly implementing some interface and accessing private fields while not implementing the interface with the outer class. It does mean inner instance points outer instance but doesn't mean the opposite. Not necesary a circular reference.
Closing the circle: Anyway there's nothing wrong with circular referencing in Java. Objects work nicely and when they're not more referenced they're garbage collected. It doesn't matter if they point each other.
The syntax you're using in the example is a little off there is no declaration of the class or interface Inner. But there isn't anything wrong with the concept of the example. In Java it will work fine.
I'm not sure what you're doing here, but you may want to consider a more simple design for maintainability etc.
It's a common pattern for anonymous event handlers to reference elements of their parent class, so no reason to avoid it if that's the case, that's how Java was designed instead of having function pointers.
(Not sure if this is what you are asking...)
At runtime, the inner class has an implicit reference to the instance of the outer class it belongs to. So whenever you pass the inner class instance around, you are also passing the outer class instance around.
You can avoid that by declaring the inner class as "static", but that means that the inner class can't access member variables of the outer class. So in that case if you want to access a member of the outer class, you need to pass it explicitly to the inner class (using a setter or using the constructor of the inner class).