Question:
accessing variables with same names in Inner class and enclosing class in java?
1970-01-01 00:00:00 UTC
accessing variables with same names in Inner class and enclosing class in java?
Three answers:
?
2016-10-17 04:49:11 UTC
comparable to what the blackcomp pronounced, you could assign enum values to variables and get right of entry to those variables, jointly with.. EnumColor BLUE = EnumColor.BLUE; EnumColor pink = EnumColor.pink; and then use BLUE, pink as variables that carry those enum values, yet... what may be the factor? To that question, i could additionally question why you're able to elect to do what you're asking to do besides? per probability if we knew that we ought to offer you greater powerful tips.
deonejuan
2009-11-12 13:43:32 UTC
lets answer the last question first. You call display() inside the method. That is called recursion and the methodolgy for recursion is the first line into the method has the condition to cancel the loop, otherwise you have an endless loop. Java fills the heap, and exit( 1) with the reason being recursion overflow.



Okay, we can only have values, states, and methods when we have an Object. that takes 'new' and a constructor. I can make an Inner with a main() in the Outer. Outer sees all the exposed members of Inner, but Inner does not see Outer.



If we subclass, then we do have parent.i and this.i.



interesting that you thought this stuff up on your own (apparently). One of the proposals for Java7 is 'closures', which allow undefined variables until the calculation call is made.



>> .============

public class PrintInnerVars {



int i;



public PrintInnerVars() {

i = 10;

display();

Inner in = new Inner();

in.display();

}

public static void main( String [] args ) {

new PrintInnerVars();





}

public void display() {

System.out.println(i);

}



class Inner {



int i;



public Inner() {

this.i = 20;

}



public void display() {

System.out.println(this.i);//here im trying to print the value of i of inner class

System.out.println(); // [1] here I want to print the value of i of enclosing class ??

// display(); // [2] here I want to call the display method of enclosing class ???

}

}

}



Outer has no 'pointer' inside Inner. A ponter is like...

Outer o = new Outer();

o <== is the pointer to the location in memory.



but, like I said, if we subclass, then we have the pointers

parent.var

this.var



hey, good luck.
Mark aka jack573
2009-11-16 09:44:07 UTC
It has been a while since I did something like this, so my memory may not be correct.



Java has a special keyword like this for accessing the outer class. The keyword is called outer.



So you would make this code:

System.out.println(this.i);//here im trying to print the value of i of inner class

System.out.println(i); // [1] here I want to print the value of i of enclosing class ??

diplay(); // [2] here I want to call the display method of enclosing class ???



into:

System.out.println(this.i);//here im trying to print the value of i of inner class

System.out.println( outer.i ); // [1] here I want to print the value of i of enclosing class ??

outer.display(); // [2] here I want to call the display method of enclosing class ???

Note I changed your typing error from diplay to display as well :)



[Edit]

My memory did fail me, so I tested it, when it didn't work I check out what it was that you do to get it to work.



Instead of using outer, you use the classname.this to get it.



So for instance, you would put enclosing.this instead of outer.



I changed the program and it compiled, but with no main method I could not run it.



Here is the changed program.



class enclosing

{

int i;

public enclosing()

{

i=10;

}

public void display()

{

System.out.println(i);

}

class inner

{

int i;

public inner()

{

this.i=20;

}

public void display()

{

System.out.println(this.i);//here im trying to print the value of i of inner class

System.out.println(enclosing.this.i); // [1] here I want to print the value of i of enclosing class ??

enclosing.this.display(); // [2] here I want to call the display method of enclosing class ???

}

}

}


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...