• 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

why a static method can directly reference non-static class???

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi guys check this code:

compiles just fine.
inner i=new outer().new inner();
How can a reference in a static method refer a non-static class without the outer classes' name?
outer.inner i=new outer().new inner();
-sriram
Edited by Corey McGlone: Added CODE Tags
[ February 17, 2004: Message edited by: Corey McGlone ]
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The rule is that you can not access a non-static member from a static context without an instance for that member. This is done because an instance member requires an instance in order to exist or to be invoked.
However, there is no problem accessing a non-static class. A class is always "available" as it needs no enclosing instance in which it must exist, unless you're talking about inner classes - which you happen to be using here. With such a case, you must have an enclosing class, which you do, but that's a different matter, entirely.
I'm not sure if I've answered your question or not, but let me know if you're still confused.
 
Sriram Chintapalli
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Corey ,
that was a nice explanation kinda what of expected but I coudnt understand this part though.
However, there is no problem accessing a non-static class.A class is always "available" as it needs no enclosing instance in which it must exist, unless you're talking about inner classes - which you happen to be using here. With such a case, you must have an enclosing class.
So do you mean a non-static inner class is always available???and what do you mean by "unless you're talking about inner classes? which you happen to be using here. With such a case, you must have an enclosing class." Are non-static inner classes and inner classes the same?
-sriram
 
Anderson gave himself the promotion. So I gave myself this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic