The difference of static and non-static inner classes? - java

I was reading Effective Java 2 - Item 22 and it says in the title:
"Favor static member classes over non-static"
but at the end of the chapter
Implementations of the collection interfaces, such as Set and List,
typically use nonstatic member classes to implement their iterators:
// Typical use of a nonstatic member class
public class MySet<E> extends AbstractSet<E> {
... // Bulk of the class omitted
public Iterator<E> iterator() {
return new MyIterator();
}
private class MyIterator implements Iterator<E> {
...
}
}
I made a test program to see if there is any difference between them and here it is.
public class JavaApplication7 {
public static void main(String[] args) {
// TODO code application logic here
JavaApplication7 t = new JavaApplication7();
Inner nonStaticObject = t.getAClass();
Sinner staticObject = new JavaApplication7.Sinner();
nonStaticObject.testIt();
staticObject.testIt();
}
public Inner getAClass(){
return new Inner();
}
static class Sinner{
public void testIt(){
System.out.println("I am inner");
}
}
class Inner{
public void testIt(){
System.out.println("I am inner");
}
}
}
The output is
I am inner
I am inner
So, they did the same job.
I wonder Why non-static class is used in this example?

An iterator usually needs to refer to the collection used to create it in the first place. You can do that with a static nested class which is explicitly provided with a reference to the collection - or you can just use an inner class, which has that reference implicitly.
Basically, if every instance of the nested class needs an instance of the enclosing class to operate (and that instance doesn't change), then you might as well make it an inner class. Otherwise, make it a static nested class.

the difference is that non-static inner class have an implicit reference to the containing class.
public class JavaApplication7 {
//You can access this attribute in non-static inner class
private String anyAttribute;
public Inner getAClass(){
return new Inner();
}
static class Sinner{
public void testIt(){
//Here, you cannot access JavaApplication7.this
}
}
class Inner{
public void testIt(){
//Here, you can access JavaApplication7.this
//You can also access *anyAttribute* or call non-static method getAClass()
}
}
}

The difference between static and non-static nested classes is that the non-static ones are implicitly associated with an instance of the outer class, which they can refer to as OuterClassName.this. This reference is useful when implementing iterators, as they need access to the members of the collection they are related to. You could achieve the same thing by using a static nested class which is explicitly handed a reference to the outer class.

there is no such thing as a static inner class, It is static nested class. "Each instance of a nonstatic [nested] class is implicitly associated with an enclosing instance of its containing class... It is possible to invoke methods on the enclosing instance."
A static nested class does not have access to the enclosing instance.
reference: this so thread

In the case of creating instance, the instance of non s
static inner class is created with the reference of
object of outer class in which it is defined……this
means it have inclosing instance …….
But the instance of static inner class
is created with the reference of Outer class, not with
the reference of object of outer class…..this means it
have not inclosing instance…
For example……
class A
{
class B
{
// static int x; not allowed here…..
}
static class C
{
static int x; // allowed here
}
}
class Test
{
public static void main(String… str)
{
A o=new A();
A.B obj1 =o.new B();//need of inclosing instance
A.C obj2 =new A.C();
// not need of reference of object of outer class….
}
}

Related

Volatile variable visibility to inner class [duplicate]

I have a class which has another static inner class:
class A {
private List<String> list;
public static class B {
// I want to update list here without making list as static
// I don't have an object for outer class
}
}
You generally use static classes when you don't need access to the instance variables. If you need to access the instance variables make the class non-static.
As you can see from other answers that you will need a non-static inner class to do that.
If you really cannot make your inner class non-static then you can add required getter and setter method in outer class and access them by creating an instance of outer class from inside inner static class:
public class A {
private List<String> list = new ArrayList<>();
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
public static class B {
// i want to update list here without making list as static
void updList() {
A a = new A();
a.setList(someOtherList);
System.out.println(a.getList());
}
}
}
No, you'll need a non-static inner class to do that.
From the JLS §8.5.1:
The static keyword may modify the declaration of a member type C within the body of a non-inner class or interface T. Its effect is to declare that C is not an inner class. Just as a static method of T has no current instance of T in its body, C also has no current instance of T, nor does it have any lexically enclosing instances.
It is a compile-time error if a static class contains a usage of a non-static member of an enclosing class.
In your code, list is an instance variable of class A and B is nested static class. The rules of accessing static and not static member don't change for nested static class.
The variable list is instance variable so a can't be accessed from static context.
To enable this, you need to change static nester class to inner class.
class A {
private List<String> list = new ArrayList<String>();
public class B {
public void someMethod(){
list.add("abc");
}
}
}
private List<String> list;
Here list is an instance variable while the inner class is static which means that class B is the same across different class A instances; one cannot access it. And, that too for a very good and obvious reason.
One solution that you can use is pass reference of member list to the constructor of class B with a weakReference to avoid memory leak.
Something like this:
class A {
private List<String> list;
public static class B {
WeakReference<List<String>> innerList;
//constructor
B(WeakReference<List<String>> innerList){
this.innerList = innerList;
}
}

Why can’t this static inner class call a non-static method on its outer class?

I'm currently reading Effective Java by Joshua Bloch and I love it! But on page 112 (Item 24) Bloch writes:
A static member class is the simplest kind of nested class. It is best
thought of as an ordinary class that happens to be declared inside
another class and has access to all of the enclosing class’s members,
even those declared private.
And that really confuses me. I would rather say:
A static member class is the simplest kind of nested class. It is best
thought of as an ordinary class that happens to be declared inside
another class and has access to all of the enclosing class’s static members,
even those declared private.
Here is a snippet that illustrates my understanding of the quote:
public class OuterClass {
public void printMessage(String message) {
System.out.println(message);
}
private static class InnerClass {
public void sayHello() {
printMessage("Hello world!"); //error: Cannot make a static reference to the non-static method printMessage(String)
}
}
}
You can see that InnerClass's sayHello method does not have access to OuterClass's printMessage method as it is declared in a static inner class while the printMessage method is an instance method. It looks like the author suggests that a static member class can access nonstatic fields of the enclosing class. I am convinced that I have misunderstood something in his last sentence but I cannot figure out what. Any help will be appreciated!
edit: I changed the visibility of the two methods because it is irrelevant to my question. I'm interested in static members, not private members.
Just because InnerClass is static, doesn't mean it couldn't obtain a reference to an instance of OuterClass through other means, most commonly as a parameter, e.g.
public class OuterClass {
private void printMessage(String message) {
System.out.println(message);
}
private static class InnerClass {
private void sayHello(OuterClass outer) {
outer.printMessage("Hello world!"); // allowed
}
}
}
If InnerClass had not been nested inside OuterClass, it would not have had access to the private method.
public class OuterClass {
private void printMessage(String message) {
System.out.println(message);
}
}
class InnerClass {
private void sayHello(OuterClass outer) {
outer.printMessage("Hello world!"); // ERROR: The method printMessage(String) from the type OuterClass is not visible
}
}
Note the error message. It's not saying you don't have access. It's saying the method cannot be called. Instance methods don't mean anything without an instance to
call them on. What the error message is telling you is that you don't have that instance.
What Bloch is telling you is that if that instance existed, code in the inner class could call private instance methods on it.
Say we have the following class:
public class OuterClass {
public void publicInstanceMethod() {}
public static void publicClassMethod() {}
private void privateInstanceMethod() {}
private static void privateClassMethod() {}
}
If we try to call those private methods from some random class, we can't:
class SomeOtherClass {
void doTheThing() {
OuterClass.publicClassMethod();
OuterClass.privateClassMethod(); // Error: privateClassMethod() has private access in OuterClass
}
void doTheThingWithTheThing(OuterClass oc) {
oc.publicInstanceMethod();
oc.privateInstanceMethod(); // Error: privateInstanceMethod() has private access in OuterClass
}
}
Note that those error messages say private access.
If we add a method to OuterClass itself, we can call those methods:
public class OuterClass {
// ...declarations etc.
private void doAThing() {
publicInstanceMethod(); // OK; same as this.publicInstanceMethod();
privateInstanceMethod(); // OK; same as this.privateInstanceMethod();
publicClassMethod();
privateClassMethod();
}
}
Or if we add a static inner class:
public class OuterClass {
// ...declarations etc.
private static class StaticInnerClass {
private void doTheThingWithTheThing(OuterClass oc) {
publicClassMethod(); // OK
privateClassMethod(); // OK, because we're "inside"
oc.publicInstanceMethod(); // OK, because we have an instance
oc.privateInstanceMethod(); // OK, because we have an instance
publicInstanceMethod(); // no instance -> Error: non-static method publicInstanceMethod() cannot be referenced from a static context
privateInstanceMethod(); // no instance -> Error: java: non-static method privateInstanceMethod() cannot be referenced from a static context
}
}
}
If we add a non-static inner class, it looks like we can do magic:
public class OuterClass {
// ...declarations etc.
private class NonStaticInnerClass {
private void doTheThing() {
publicClassMethod(); // OK
privateClassMethod(); // OK
publicInstanceMethod(); // OK
privateInstanceMethod(); // OK
}
}
}
However, there's trickery going on here: a non-static inner class is always associated with an instance of the outer class, and what you're really looking at is:
private class NonStaticInnerClass {
private void doTheThing() {
publicClassMethod(); // OK
privateClassMethod(); // OK
OuterClass.this.publicInstanceMethod(); // still OK
OuterClass.this.privateInstanceMethod(); // still OK
}
}
Here, OuterClass.this is special syntax for accessing that outer instance. But you only need it if it's ambiguous, e.g. if the outer and inner classes have methods with the same name.
Note too that the non-static class can still do the things the static one can do:
private class NonStaticInnerClass {
private void doTheThingWithTheThing(OuterClass oc) {
// 'oc' does *not* have to be the same instance as 'OuterClass.this'
oc.publicInstanceMethod();
oc.privateInstanceMethod();
}
}
In short: public and private are always about access. The point Bloch is making is that inner classes have access that other classes don't. But no amount of access allows you to call an instance method without telling the compiler what instance you want to call it on.
The way you showed it requires inheritance. But methods and fields could be access in this way:
public class OuterClass {
private void printMessage(String message) {
System.out.println(message);
}
private static class InnerClass {
private void sayHello() {
OuterClass outer = new OuterClass();
outer.printMessage("Hello world!");
}
}
}
But, that the static inner class doesn't have access to the printMessage function doesn't have to do with that it is an inner class, but that it is static and can't invoke a non-static method. I think that the use of the word "static" you proposed was implicit in the first sentence. What he is pointing out, or chose to emphasize, is just that the inner class can still access private methods of its parent class. He might have just though it unnecessary or confusing to make the static/non-static distinction in the same sentence, too.
The way I see it, the text is absolutely right. Static member classes can access the private members of the enclosing classes (sort of). Let me show you an example:
public class OuterClass {
String _name;
int _age;
public OuterClass(String name) {
_name = name;
}
public static OuterClass CreateOuterClass(String name, int age) {
OuterClass instance = new OuterClass(name);
instance._age = age; // Notice that the private field "_age" of the enclosing class is visible/accessible inside this static method (as it would also be inside of a static member class).
return instance;
}
}

Why am I throwing no errors for calling object-specific methods/fields from inner classes?

I'm using something similar to the following code in one of my Java classes:
public class SomeClass {
private int someValue;
void incrementValue() {
someValue++;
}
public abstract static class InnerClass {
private final SomeClass toRunOn;
public InnerClass(SomeClass obj) {
toRunOn = obj;
}
public abstract void execute();
// To allow us to call this on a given instance
final SomeClass getObj() {
return toRunOn;
}
}
public final InnerClass called = new InnerClass(this) {
public final void execute() {
incrementValue(); // This is what I thought should be throwing an error
}
};
}
However, while I would expect this to throw a compiler error in the called field defining execute() due to me not giving incrementValue() an object to work on (which is why I allowed for passing this to the inner class), it is completely fine with it. I'm uncertain why this is not giving me an error, and further confused as to what instance it would be calling on.
Am I misunderstanding some form of reference calling here, or is something more subtle going on?
called is assigned an instance of an anonymous sub-class of InnerClass. Since it's an instance member, it is initialized when an instance of SomeClass is created.
Since it is declared in the body of SomeClass, it is an inner class of SomeClass and has access to the instance methods and members of SomeClass. incrementValue() will be executed on the instance of SomeClass for which the called member was initialized.
Perhaps it would be easier to understand if you replace the anonymous class with an equivalent regular inner class :
public class SomeClass {
....
class SubInnerClass extends InnerClass {
public final void execute() {
incrementValue();
}
}
public final InnerClass called = new SubInnerClass(this);
....
}

Java: How to check if an object is an instance of a non-static inner class, regardless of the outer object?

If I have an inner class e.g.
class Outer{
class Inner{}
}
Is there any way to check if an arbitrary Object is an instance of any Inner, regardless of its outer object? instanceof gives false when the objects are not Inners from the same Outer. I know a workaround is just to make Inner a static class, but I'm wondering if what I'm asking is possible.
Example:
class Outer{
Inner inner = new Inner();
class Inner{}
public boolean isInner(Object o){
return o instanceof Inner;
}
}
Outer outer1 = new Outer();
Outer outer2 = new Outer();
boolean answer = outer1.isInner(outer2.inner); //gives false
And what about?
public static boolean isInnerClass(Class<?> clazz) {
return clazz.isMemberClass() && !Modifier.isStatic(clazz.getModifiers());
}
The method isMemberClass() will test if the method is a member (and not an anonymous or local class) and the second condition will verify that your member class is not static.
By the way, the documentation explains the differences between local, anonymous and nested classes.
Nested classes are divided into two categories: static and non-static. Nested classes that are declared static are simply called static nested classes. Non-static nested classes are called inner classes.
o instanceof Outer.Inner gives false when o is an instance of an Inner of any Outer other than the one you're calling it from.
This doesn't happen for me - I get true for o instanceof Inner regardless of which particular enclosing instance of Outer the o belongs to:
class Outer {
class Inner {}
void test() {
// Inner instance that belongs to this Outer
Inner thisInner = new Inner();
// Inner instance that belongs to a different Outer
Outer other = new Outer();
Inner otherInner = other.new Inner();
// both print true
System.out.println(thisInner instanceof Inner);
System.out.println(otherInner instanceof Inner);
}
public static void main(String[] args) {
new Outer().test();
}
}
Tested with both Java 6 and 7.
Did you try using getEnclosingClass():
Returns the immediately enclosing class of the underlying class. If the underlying class is a top level class this method returns null.
Outer.class.equals(object.getClass().getEnclosingClass())
Getting the correct enclosing class of the object , IMHO is not so easy . Read this.
Somewhat of a hack would be :
object.getClass().getName().contains("Outer$");
you could always:
getClass().getName()
and do a String comparison.
EDIT : to account for inheritance (among inner classes? who would do that?!) you could always loop through getSuperclass() and check for them as well, and even go after implemented interfaces.
The java.lang.Class.getEnclosingClass() method returns the immediately enclosing class of the underlying class. If this class is a top level class this method returns null.
The following example shows the usage of java.lang.Class.getEnclosingClass() method:
import java.lang.*;
public class ClassDemo {
// constructor
public ClassDemo() {
// class Outer as inner class for class ClassDemo
class Outer {
public void show() {
// inner class of Class Outer
class Inner {
public void show() {
System.out.print(getClass().getName() + " inner in...");
System.out.println(getClass().getEnclosingClass());
}
}
System.out.print(getClass().getName() + " inner in...");
System.out.println(getClass().getEnclosingClass());
// inner class show() function
Inner i = new Inner();
i.show();
}
}
// outer class show() function
Outer o = new Outer();
o.show();
}
public static void main(String[] args) {
ClassDemo cls = new ClassDemo();
}
}
Output
ClassDemo$1Outer inner in...class ClassDemo
ClassDemo$1Outer$1Inner inner in...class ClassDemo$1Outer
I was googling for finding out better answers, to find out that there are none out there.
Here is what I have which works pretty well:
public static boolean isStatic(Class klass) {
return Modifier.isStatic(klass.getModifiers());
}
/**
* Non static inner class
*/
public static boolean isInnerclass(Class klass) {
return klass.getDeclaringClass() != null && !isStatic(klass);
}
Will return true for local inner classes. isMemberClass and others do not work for this purpose.

Nested class(static inner) lifetime/scope

I declared a Static Inner class, of which I am creating a new instance in a method of Outer class. But, I am getting result suggesting that the same instance of Inner class is used everytime in my method.
Example Below,
public class Outer{
public method m(){
Inner n = new Inner(); //Creating local instance of Nested class
n.something();
}
public static class Inner{
Map<K,V> cache = new Map<K,V>();
void something(){
//use and update cache;
}
}
}
public Test{
public static void main(String a[]){
Outer o = new Outer();
o.m();
o.m(); //cache was still available
}
}
Can someone help with why two instance of Inner class are not created?
Also, should this behaviour change if I remove static from inner class?
static inner classes can be instantiated only once per outer enclosing class instance.Since you have only one Outer o = new Outer(); outer instance o.m(); will call same instance of inner class.
Create two instances like this
Outer o = new Outer();
o.m();
Outer o1 = new Outer();
o1.m(); // will create new instance of inner
Regarding static key word
Nested classes can be: static and non-static. Nested classes that are declared static are s static nested classes. Non-static nested classes are called inner classes.
A static nested class does not have a reference to a nesting instance, so a static nested class cannot access non-static methods of an instance of the class in which it is nested.

Categories