• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

inner class question

 
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can I access inner class ten and it's integer variable x = 11 ?

CertPal Test

 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
package testscripts;
public class Test
{
final Integer y=10;
class two
{
final int x;
public two()
{
x = 10;
class ten
{
int x = 11;
}
ten t1=new ten();
System.out.println("ten x value is"+t1.x);
}
}
public int get()
{
two two2 = new two();
return two2.x;
}
public static void main(String [] args)
{
Test one = new Test();
two t=one.new two();
System.out.println("X value is"+t.x);


System.out.println(one.y + one.get());

}
}
 
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If ten is a class outside two's constructor, the code to access ten will be:


But since ten is a class defined inside two's constructor, I don't think there is any way to access it.
 
Eshwin Sukhdeve
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Helen you cant access the class two from outside.because it is local for the constructor.
 
Helen Ma
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah.... I just modified my post. I just realized that ten is a class defined inside two's constructor.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Janki Shah wrote:How can I access inner class ten and it's integer variable x = 11 ?

CertPal Test






hi friend,


i have tried the pgm what you have posted here. i have cleared your problem. check the below program:




public class one
{
final Integer y=10;
two t=new two();
public class two//class inside class one
{

final int x;
public two() //constructor of class two
{
x = 10;

class ten //sub-class in class two
{
ten(){
int x = 11;
System.out.println("inside class ten"+x);
}
}
ten t=new ten();

System.out.println("inside class two"+x);
}
}
public int get() // method in class one
{
System.out.println("below two stmts output is class one inside get() method");
two two2 = new two();
return two2.x;

}
public static void main(String [] args)
{
one one = new one();
System.out.println("sum of two no.s is:"+(one.y + one.get()));

}
}
output is:

inside class ten11
inside class two10
below two stmts is of class one inside get() method
inside class ten11
inside class two10
sum of two no.s is:20
 
Janki Shah
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Eshwin Sukhdeve wrote:Helen you cant access the class two from outside.because it is local for the constructor.



So, if the ten class is ouside the two class's constructor but in side the two class than we can do this,

But if the class is inside the two class's constructor we can not use it. So, is this true always that you can not use any class's constructor code on an instance of that class even if there is an inner class defined in ?
 
Helen Ma
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Janki.
I have the same question as yours.
Since ten is an inner class defined inside Two's constructor, I don't know how to create or access an instance of ten.

Can anyone help?
 
Ranch Hand
Posts: 859
IBM DB2 Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Aside, ALL Class names should start with uppercase!

Pat.
 
Ranch Hand
Posts: 71
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think class inside a construtor is just like method level local class which is for the purpose of use and throw, don't need any more. but if you really need the ten's variable x any where outside the ten class, declare a new variable and store that into it. here is the code.



 
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
These are the class files that I found after compiling the One class

1) One.class
2) One$two.class
3) One$two$1ten.class

In short, ten is an anonymous inner class, you cant access it past its place of use.
 
Praveen Kumar M K
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yep - that's stupid! Please ignore the previous post.
 
I didn't do it. You can't prove it. Nobody saw me. The sheep are lying! This tiny ad is my witness!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic