Nested class(static inner) lifetime/scope - java

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.

Related

Using a non static data member in to a non static nested class withourt using object

class Outer
{
int x=10;
class Inner
{
void show()
{
System.out.println(x);
}
}
public static void main(String args[])
{
Outer obj=new Outer();
Inner obj1=new Outer().new Inner();
obj1.show();
}
}
I tried making a non static nested class and tried to use non static data member of outer class in non static inner class. I did not get that if x is non static, how i am using it without object. Kindly give me the answer?
You're not using it without an object. Inner (non-static nested) classes have a reference to the outer object, whose x is used.
Inner class is just a syntactic sugar to have an implicit reference to an outer class. Internally (after javac compilation) your class Inner looks like this:
static class Inner
{
private final Outer this$0;
public Inner(Outer outer) {
this$0 = outer;
}
void show()
{
System.out.println(this$0.x);
}
}
And when you write Inner obj1=new Outer().new Inner(); the compiler changes it to something like Inner obj1=new Inner(new Outer());.

Call inner class member from a different class

Please forgive me if this question is silly as i'm completely new to JAVA program. I'm looking into nested classes concept and come across the following program.
// Demonstrate an inner class.
class Outer {
int outer_x = 100;
void test() {
Inner inner = new Inner();
inner.display();
}
// this is an inner class
class Inner {
void display() {
System.out.println("Display: outer_x = " + outer_x);
}
}
}
class NestedClass {
public static void main(String args[]) {
Outer outer = new Outer();
outer.test();
// Inner inner = new Outer().Inner();
// inner.display();
}
}
And my doubt is how to access members of Inner class from NestedClass. In "Java - The complete reference", it is given that "You can, however, create an instance of Inner outside of
Outer by qualifying its name with Outer, as in Outer.Inner". But if i try to use it as,
Inner inner = new Outer().Inner();
inner.display();
it is throwing error. So please help me experts.
You need to create a new Inner instance by using the new keyword.
Inner inner = new Outer().new Inner(); // "new" keyword is required to create a new Inner instance.
If you do not have the import for import com.java.test.Outer.Inner; added, add it. Or else, you can do something like this
Outer.Inner inner = new Outer().new Inner();
Outer st = new Outer();
Outer.Inner fl = st.new Inner();
Note that the code above would be exactly the same whether the main() method is inside the outer class (because main is a static method) or even in some other class. Some other class could only run the code if it has access to outer class . But, Outer Class will have default, or package access since access modifiers are not specified when Outer Class is declared. This essentially means that any class within the same package as OuterClass will be able to run the code above without any issues.
You first have to create an instance of the outer class. After that, you can create an instance of that nested inner class by the outer class' instance.
Outer outer = new Outer();
Outer.Inner a = outer. new Inner();
a.display();
Inner class can be accessed only through live instance of outer class.
Try this:
class NestedClass {
public static void main(String args[]) {
Outer outer = new Outer();
outer.test();
Outer.Inner inner = outer.new Inner();
inner.display();
}
}
Use this:
Outer.Inner inner=new Outer().new Inner();
as you need to create object of Inner class too as it is not static inner class.
So your code becomes:
class NestedClass {
public static void main(String args[]) {
Outer outer = new Outer();
outer.test();
Outer.Inner inner = outer.new Inner();
inner.display();
}
}

Can't create an instance of a Class

What's the reason why - an enclosing instance that contains appears when trying to instantiate a class?
Below is my actual code:
public static void main(String[] args) {
InterspecTradeItems_Type.Item_Type item = new InterspecTradeItems_Type.Item_Type();
// Error: an enclosing instance that contains InterspecTradeItems_Type.Item_Type is required
}
public class InterspecTradeItems_Type {
public class Item_Type {
}
}
Thanks.
As Item_Type is inner class. To instantiate an inner class, you must first instantiate the outer class. Then, create the inner object within the outer object with this syntax:
InterspecTradeItems_Type.Item_Type item = new InterspecTradeItems_Type().new Item_Type();
Since Item_Type class is not a static nested class, but an inner class of InterspecTradeItems_Type, you need an instance of the later to access the former.
So, to create an instance of the inner class, you should create instance of the enclosing class:
new InterspecTradeItems_Type().new Item_Type();
Of course another option is to make Item_Type a static class:
public class InterspecTradeItems_Type {
public static class Item_Type {
}
}
And then your code would work just fine.
Assuming InterspecTradeItems_Type is declared/defined in a class called Main, you need
InterspecTradeItems_Type.Item_Type item = new Main().
new InterspecTradeItems_Type().new Item_Type();
You have an inner class within and inner class. You need an instance of each outer class to get to it.
try
public static void main(String[] args) {
InterspecTradeItems_Type item = new InterspecTradeItems_Type();
Item_Type item1 = item.new Item_Type();
}
The correct way to have an object of inner class is
InterspecTradeItems_Type.Item_Type item = new InterspecTradeItems_Type.new Item_Type();
To instantiate an inner class, you must first instantiate the outer class. Then, create the inner object within the outer object with this syntax:
OuterClass.InnerClass innerObject = outerObject.new InnerClass();
So either use above syntax or make Item_Type class static
public static void main(String[] args) {
InterspecTradeItems_Type.Item_Type item = new InterspecTradeItems_Type.
Item_Type();
// Error: an enclosing instance that contains
InterspecTradeItems_Type.Item_Type is required
}
public class InterspecTradeItems_Type {
public static class Item_Type {
}
}
Read docs on Inner classes for more information.

The difference of static and non-static inner classes?

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

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.

Categories