we have a class having a property.... in that class i have a inner class with having same property name...
if i print that property in inner class then it will show the value of inner class..
i am surprised by the to class property of outer class.. because its use this with class name....i don't know why and how this can be behave as static .
Example
public class Super {
int x=10;
class nest
{
int x=20;
public void show()
{
int t=Super.this.x;
System.out.println(t);
}
}
public static void main(String a[])
{
Super n=new Super();
nest s=n.new nest();
s.show();
}
}
The syntax OuterClassName.this.fieldname is used to refer to a field or method in the outer class instance from within the inner class. I dont see why you would consider it behaving as static.
Related
I have a super and a sub class here , I'm trying to learn upcasting.
class Demo1{
int x=10;
void show(){
System.out.println(this.x);
System.out.println(this.s);
}
}
and the Child class:
class Demo2 extends Demo1{
int ashish=200;
String s="Nana";
public static void main(String... args){
Demo1 d=new Demo2();
d.show();
}
}
Here, I'm trying to print String s. Why does it shows a compilation error that the variable doesn't exist?
By my knowledge, during upcasting .. the object instance created has all the data members of the child class and its reference is given to parent class variable, so why doesn't this referencing to the data members of Demo2?
class Demo1{
int x=10;
void show(){
System.out.println(this.x);
System.out.println(this.s);
}
}
Demo1 has to be able to exist as an independent unit. If you created an instance of Demo1, by itself, it would not have any s, so this would be nonsensical.
What you could do, similar to what you're trying to do, would be something like
abstract class Demo1{
int x=10;
abstract String s();
void show(){
System.out.println(this.x);
System.out.println(this.s());
}
}
class Demo2 extends Demo1 {
String s = "foo";
public String s() { return s; }
}
This makes it explicit that Demo1 has a "hole" -- an implementation detail subtypes need to fill in, to tell it how to get s.
The super class doesn't know anything about the members (fields and methods) of any subclass. So, using the s field in super class is not allowed, thus you get the compiler error.
i want to know how i can access to a inner class, which is in an Enum.
Example:
public enum myEnum{
public class myInnerClass{
public void aMethod(){
//do somethink.....
}
}
}
How can i access to this class(access to it methods) in another class?
Thanks for your help :)
You can access the inner class inside the enum using it's instance, defined by enum fields:
public enum MyEnum{
INSTANCE_A,
INSTANCE_B;
public class MyInnerClass {
// This is no different from the inner class in a normal class
public String show() {
// You can get the name of the instance for which this method was called.
System.out.println(MyEnum.this.name());
return "Hello";
}
}
}
Now, to create an instance of MyInnerClass, you would do:
MyEnum instanceA = MyEnum.INSTANCE_A;
MyEnum.MyInnerClass myInnerInstance = instanceA.new MyInnerClass();
System.out.println(myInnerInstance.show());
Output:
INSTANCE_A // For MyEnum.this.name()
Hello
The way is similar to how you would do for an inner class, which is inside a normal class. There is no difference.
I mean calling methods of the inner class
To be able to call methods of inner class you'll need an instance of inner class (except for the case when inner class is static and you want to call a static method).
To create an object of innerClass you can do SSS.INSTANCE.new A(); (class from below)
Or you can declare it static then you'll work with it as with normal class.
public enum SSS {
INSTANCE;
public static class A {
}
public static void main(String[] args) {
SSS.A a = new SSS.A();
}
}
try this
Class cls = myEnum.myInnerClass.class;
public class InnerClass {
class Inner
{
public void method()
{
System.out.println("Innerclass");
}
}
}
class Sample extends InnerClass.Inner
{
public static void main(String [] arg)
{
Sample s = new Sample(new InnerClass());
s.method();
}
//why is this mandatory???
Sample(InnerClass i) {
i.super();
}
#Override
public void method() {
System.out.println("derived class");
}
}
when i make a class that derives from an innerclass (Innerclass.Inner) default constructor doesn't works. later i came to know that it requires to include a constructor taking Enclosing class reference why is it so?
Non static inner classes in Java have an implicit reference to the enclosing instance. You can solve your problem with:
public class InnerClass {
static class Inner // can make it public too
{
public void method()
{
System.out.println("Innerclass");
}
}
}
Just don't expect to be able to call any methods on InnerClass without a specific instance.
Because non-static inner classes have an implicit member that points back to their outer class, and you can't create an instance of the inner class without giving it that pointer. If you directly create an instance of an inner class, you have to use new outer.Inner() (or it might be outer.new Inner(), I can never remember). But Sample isn't an inner class, it just inherits one, so the outer instance must be passed in its constructor to the base constructor. Thus, it needs to have some instance of outer available, or create it itself.
public class Test {
public static void main(String args[]){
Test t = new Test();
t.inner();
}
public final void print() {
System.out.print("main");
}
public void inner() {
class TestInner {
void print(){
System.out.print("sub");
}
}
TestInner inner =new TestInner();
inner.print();
print();
}
}
Out put : submain
Question : the method print() in class Test is final and is accessible to local class , but still local class is a able to define print() method again how?
If we declare private final x() in super class, it is not accessible in sub class so we can define x() in sub class.
If we declare public final x() in super class, it is accessible in sub class so we can not define x() in sub class.
Can anybody explain ?
The inner class is not overriding the final method.
The inner class would have to extend the outer class for it to be able to override a method from the outer class.
The two classes are separate and unrelated to each other, other than the outer class contains the inner one.
Because the TestInner class doesn't extend the Test class, so it has its own namespace that is separate.
There is no trick to it, it isn't overwriting the Test classes print method.
The inner class is not overriding the final method.
The inner class would have to extend the outer class for it to be able to override a method from the outer class.
The two classes are separate and unrelated to each other, other than the outer class contains the inner one.
The following Java program just calculates the area of a circle. It uses the concept of inner classes available in Java. One of the inner classes (FirstInner) inherits it's enclosing class named Outer and the SecondInner class derives the FirstInner in turn. The program is working just fine. There is no problem at all. Let's have look at it.
package innerclass;
import innerclass.Outer.SecondInner; // Need to be inherited.
import java.util.Scanner;
class Outer
{
protected double r;
public Outer()
{
}
public Outer(double r)
{
this.r=r;
}
public class FirstInner extends Outer
{
public FirstInner(double r)
{
super(r);
}
}
final public class SecondInner extends FirstInner
{
public SecondInner(double r)
{
Outer.this.super(r); //<-------------
}
public void showSum()
{
System.out.print("\nArea of circle = "+(Math.pow(r, 2)*Math.PI)+"\n\n");
}
}
}
final public class Main
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
System.out.print("\nEnter radius:->");
double r=s.nextDouble();
Outer o=new Outer();
SecondInner secondInner = o.new SecondInner(r);
secondInner.showSum();
}
}
Now in the SecondInner class, I'm qualifying it's super class which is the FirstInner class first with this and again with Outer like Outer.this.super(r); which simply looks like just super(r);.
The use of only super(r) rather than Outer.this.super(r); causes a compiler-time error indicating that "cannot reference this before supertype constructor has been called". Why is it so? I mean why I have to use Outer.this.super(r); rather than just super(r)?
One more point when I make the FirstInner class static, the program issues no compile-time error and allows to use just super(r) in place of Outer.this.super(r);. Why?
I get a different error from my Eclipse environment:
"No enclosing instance of type Outer is available due to some intermediate constructor"
This one is clearer and can be linked to the fact that you cannot instantiate a non-static inner class before the outer class has been instantiated.
Please see the example that is described here.
15.11.2 Accessing Superclass Members using super
From the java tut
http://download.oracle.com/javase/tutorial/java/javaOO/nested.html
An instance of InnerClass can exist only within an instance of
OuterClass and has direct access to the methods and fields of its
enclosing instance.
Going by that statement, the following approach makes sense. You are accessing the instance using "this" by resolving with the help of the class name, which is defined here
in primary expressions .
http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#20860
class SecondInner extends FirstInner
{
public SecondInner(double r)
{
Outer.this.super(r); //<-------------
}
public void showSum()
{
System.out.print("\nArea of circle = "
+(Math.pow(r, 2)*Math.PI)+"\n\n");
}
}
}
For example if your SecondInner were to be declared within FirstInner it has to be accessed using FirstInner.this.super()