For example,if A has inner class B,B has inner class C,all have a property "name",I know C can access name in B by B.this.name,but how to access name in A from C?
public class A{
String name="A";
public class B{
String name="B";
public class C{
String name="C";
public C(){
//how to print name in A?
//System.out.println(B.A.name);
//System.out.println(B.A.this.name);
//System.out.println(B.this.A.name);
//System.out.println(B.this.A.this.name);
}
}
C c=new C();
}
B b=new B();
public static void main(String[] args){
new A();
}
}
I tried so many syntax but they cannot compile,also when search java outer class,I found most of them are about outer class only, not outer outer class.
Use A.this.name to access the outer most class. Or any other class.
Using System.out.println(A.this.name);
Related
I tried below code and according to it I have the understanding that inner class is inherited to OuterClass's subclass.Is it correct?
class Outter{
int i=1;
class Inner{
int i=2;
public void hello(){
System.out.println("hello from outter.inner");
}
}
}
class ChildClass extends Outter{
class ChildInner{
}
}
public class Classes {
public static void main(String[] args) {
Inner inner = (new ChildClass()).new Inner();
System.out.println(inner.i);
inner.hello();
}
}
Output as excepted:
2
hello from outter.inner
Inner inner = (new ChildClass()).new Inner();
As this line of code worked it should mean that Inner class is inherited to ChildClass
I am getting confused because of the below statement I found on Link
When an outer class is extended by it’s sub class, Member inner classes will not be inherited to sub class. To use inner class properties inside the sub class of outer class, sub class must also have an inner class and that inner class must extend inner class of the outer class.
So I will illustrate that statement with an example:
When an outer class is extended by it’s sub class, Member inner
classes will not be inherited to sub class. To use inner class
properties inside the sub class of outer class, sub class must also
have an inner class and that inner class must extend inner class of
the outer class.
class Outter {
void method(){
Inner test=new Inner();
test.i=5; //No problem to do that even if i is private because it is inner class
}
class Inner {
private int i = 2;
}
}
class ChildClass extends Outter{
void method2(){
Inner test=new Inner();
test.i=5; //Does not compile
}
}
You cannot access "i" in the child class. And if you also extend the inner there you can:
class ChildClass extends Outter{
void method2(){
Inner2 test=new Inner2();
test.i=5; //Compiles fine because we have also extended the inner class (like written in the quoted text)
}
class Inner2 extends Inner{ }
}
The statement:
Inner inner = (new ChildClass()).new Inner();
Is not really inherited to ChildClass
If you break it you are basically doing this:
ChildClass child = new ChildClass();
Inner inner = child.new Inner();
Now you can call new Inner() on the ChildClass because it extends Outter
This doesn't mean that ChildClass can call any of the methods/properties inside Inner just because Inner is part of Outter
When we define inner classes, I understand that static inner classes are accessed with the outer class and inner member class exist with the instance of the outer class.
The confusion is, if I want to hold an instance of an non-private inner member class, then the variable declaration goes like:
OuterClass.InnerClass innerObject = outerObject.new InnerClass();
How outer class is able to reference the inner member class like this? What is happening here?
Sample Code:
public class NestedClasses {
public static void main(String[] args) {
A.B b = new A().new B(); // <- How A is directly accessing B, B is not defined as static.
A.StaticClass staticClass = new A.StaticClass();
}
}
class A {
static int x;
int y;
A() {
System.out.println(this.getClass().getName());
}
static class StaticClass {
StaticClass() {
System.out.println(this.getClass().getName());
}
}
class B {
B() {
System.out.println(this.getClass().getName());
}
}
}
I confused if
Abstract Class A{method();method2();}
And Other Class B Which Have Inner Class C
Class B{Abstract Class C{method(){//body}}}
And now Question is how to extends Class C b/C Abstract Class must be extends else
this is Unused class.
First, let's make it simpler - this has nothing to do with Android directly, and you don't need your A class at all. Here's what you want:
class Outer {
abstract class Inner {
}
}
class Child extends Outer.Inner {
}
That doesn't compile, because when you create an instance of Child you need to provide an instance of Outer to the Inner constructor:
Test.java:6: error: an enclosing instance that contains Outer.Inner is required
class Child extends Outer.Inner {
^
1 error
There are two options that can fix this:
If you don't need to refer to an implicit instance of Outer from Inner, you could make Inner a static nested class instead:
static abstract class Inner {
}
You could change Child to accept a reference to an instance of Outer, and use that to call the Inner constructor, which uses slightly surprising syntax, but works:
Child(Outer outer) {
// Calls Inner constructor, providing
// outer as the containing instance
outer.super();
}
Note that these are alternatives - you should pick which one you want based on whether or not the inner class really needs to be an inner class.
You simply extend it
class B{abstract class C{abstract void method();}}
class D extends B{
class E extends C{
void method(){
System.out.println("Hello World");
}
}
}
Or slightly more complicated without extending outer class
class B{abstract class C{abstract void method();}}
public class F extends B.C{
F(B b){
b.super();
}
void method(){
System.out.println("Hello World");
}
public static void main(String[] args){
B b = new B();
F f = new F(b);
f.method();
}
}
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.
Trying to create 1 interface and 2 concrete classes inside a Parent class. This will qualify the enclosing classes to be Inner classes.
public class Test2 {
interface A{
public void call();
}
class B implements A{
public void call(){
System.out.println("inside class B");
}
}
class C extends B implements A{
public void call(){
super.call();
}
}
public static void main(String[] args) {
A a = new C();
a.call();
}
}
Now I am not really sure how to create the object of class C inside the static main() method and call class C's call() method.
Right now I am getting problem in the line : A a = new C();
Here the inner class is not static, so you need to create an instance of outer class and then invoke new,
A a = new Test2().new C();
But in this case, you can make the inner class static,
static class C extends B implements A
then it's ok to use,
A a = new C()
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 you need to use :
A a = new Test2().new C();
Refer the Java Tutorial.
You should do this
A a = new Test2().new C();