Can outer classes call methods of its inner class? - java

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.

Related

Singleton-pattern destroyable by inner class?

I wrote a class following the Singleton-pattern and would like to write a inner class for it next.
What worries me is that the inner class is able to access all of the outer classes private fields, including the private field for the single instance and i suppose the private constructor likewise.
Is it possible to attack the singleton and to make a second instance of it when one has access to the constructor of the inner class?
I was thinking of some kind of navigating from an instance of the inner class to the constructor of the class.
So for example:
class Outer{
private static Outer instance;
private Outer(){}
public static Outer getInstance(){
if(instance==null)instance= new Outer();
return instance;
}
class Inner{
public Inner(){}
}
and now something like:
public class Main{
public static void main(String[]args){
Outer outer = Outer.getInstance();
Inner inner = outer.new Inner();
Outer outer2 = inner.Outer.this.new Outer();
}
}
Noticing that the last line is not compilable I thought there might be some other way to "navigate" to the constructor of the outer class once one has an object of the inner class.
What worries me is that the inner class is able to access all of the
outer classes private fields, including the private field for the
single instance and i suppose the private constructor likewise.
That's the whole purpose in life of inner class, read more here, inner classes are meant for the situation when you want to keep some state of a class as private but still want a cohesive class to access it, so in those situation you make an inner class for that top level class.
Is it possible to attack the singleton and to make a second instance
of it when one has access to the constructor of the inner class?
If you have implemented proper singleton pattern for your top level class then no-one can create another instance of your top level class.
Also, just in case if you are thinking otherwise then when you create an instance of inner class then it doesn't create an instance of outer class, no there again there are scenarios like if you have a static inner class then you do need an instance of top level class but if you have a non-static inner class then first you need to create a instance of top level class and then you create instance of your inner class, but whole point is that creating an instance of inner class doesn't create another instance of outer class, so if you have implemented singleton pattern properly in our outer class then you are fine.
I would recommend you to read about inner classes here.
Singleton-pattern destroyable by inner class?
With above explanation, answer is no, with a properly implemented singleton pattern in top level class, an inner of it cannot create another instance of the top level class.
Your question seems to indicate that you're trying to prevent an attacker from making specific changes in your source code. If someone has that level of access already, there's no security on Earth that can protect you from an attack.

Inheritance and inner classes in java?

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.

How to instantiate an inner class without instantiating the outer

I've been following this tutorial for creating a database and have one question. It says "To access your database, instantiate your subclass of SQLiteOpenHelper:
FeedReaderDbHelper mDbHelper = new FeedReaderDbHelper(getContext());". But how can I instantiate my FeedReaderDbHelper class in another class (in a service in my case) without instantiating the whole FeedReaderContract class, because it says I should avoid instantiating the former class? In my service I am doing this:
FeedReaderContract.FeedReaderDbHelper mDbHelper = new FeedReaderContract().new FeedReaderDbHelper(mContext);
But the tutorial says it's forbidden.
If your inner class is associated to an instance of your outer class, you wouldn't be able to do that. IOW, if your inner class is not a static inner class, then it really doesn't make sense to create your inner class alone, right?
On the other hand, you can change your inner class to a static inner class. But you have to be careful if this is what you need since you are basically saying that the static inner class is not dependent on any one instance of the outer class.
Check this out: https://stackoverflow.com/a/70358/2231632

Java - Inner class constructor - allowed for outer class only

I have inner class in my code. I want to give public access to its instances, but only outer class should be able to create this instances, like in "private" access. Is it possible without making properly small package (or creating public interface for every such inner class)?
(Sorry if my english is bad :P)
It is possible. Declare your inner class public, but its constructor private. This way you can create it only inside your enclosing class and itself, but not from outside.
By default,If you want to get the instance of the inner class you need to have the Outer class first.
A inner class is a member of its enclosing class.
You need not to do anything for that.
Non-static nested classes (inner classes) have access to other members of the enclosing class, even if they are declared private
I hope I understood your question in right way.
Please refer.
So make private of inner class.
public class Outer {
private class Inner {}
public String foo() {
return new Inner().toString();
}
}
you can't legally call the private default constructor because it is private

How do you resolve a circular dependency with an inner class?

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

Categories

Resources