• 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

doubt in inner classes..

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
code:


class Hat{
int k=20;
int l=100;
private int m=56;
class Qute{
int show() {System.out.println(m);
return m;}
}
class Hello{
void display() {
//int d=Qute.show();----------line 1
System.out.println(d);}
} }
class Hate{
public static void main(String args[ ]){

Hat.Qute t=new Hat().new Qute();
t.show();
Hat.Hello j=new Hat().new Hello();
j.display();
}
}

why is line 1 giving a compiler error...as per my knowledge a non static member class can directly refer to any member(including nested)of the enclosing class..
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes you are right that a non-static inner class can access any members of the enclosing class. But here you are trying to access a non-static member of an inner class from another inner class. For that you need to have an instance of the inner class.

Look at your modified code



If you assume Qute as a top level class then to access non-static members of Qute from Hello, you will need to create an instance of Qute class...
 
disha arora
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ohho.....such a silly mistake..i did
anyways thankz a lot.....ankit
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now that you have got this one, I would just like to get in depth here. As you know that to create an instance of non-static inner class, you need an outer class instance. Here when I created an instance of Qute from Hello, I didn't use any outer class Instance. This is because Hello is itself a non-static inner class(let's call it xx). So it also has an enclosing class instance associated with it. So since we did not provide any outer class instance with the syntax instance.new, so compiler automatically used xx to create the instance of Qute. so actually the declaration in display would work as

Qute qt = xx.new Qute();
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic