• 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 basics. (Marcus Green's mock exam 2)

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From Marcus Green's mock exam 2:
Which of the following statements are true?
1) Adding more classes via import statements will cause a performance overhead, only import classes you actually use.
2) Under no circumstances can a class be defined with the private modifier
3) A inner class may under some circumstances be defined with the protected modifier
4) An interface cannot be instantiated
I said 4.
Marcus Green says 3,4
Cant inner class be ALWAYS defined with the protected modifier??
Thnx in advance,
Amol
 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Not always:
A inner class may under some circumstances be defined with the protected modifier
Inner class can also be declared as public,private,default access modifier.
Jamal Hasanov
www.j-think.com
 
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Cant inner class be ALWAYS defined with the protected modifier??


Not if it's local or anonymous.
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


A inner class may under some circumstances be defined with the protected modifier


can any one explain this with an example?
Thanks
bani
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by bani kaali:
can any one explain this with an example?


Here ya go...a protected inner class:

I hope that helps,
Corey
 
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Not if it's local or anonymous.


what�s a local inner class?
Francisco
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A local inner class is a class declared within the scope of a method. Whether it is considered static or not (as far as access considerations are concerned) depends on whether the enclosing method is static or not as well. Also, only local variables declared as final are accessible by the local class.
Here's a simple example:

[ June 25, 2002: Message edited by: Anthony Villanueva ]
 
Francisco A Guimaraes
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just one thing Anthony...y is accessible, look:

will produce this output:
Hi!
x: 7
y: 8
b: 2
Francisco
 
amol kat
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought "local inner class" is called a "local class" and inner class is always that top level thing which if is static is called a top-level nested class and if not static is called an inner class.
 
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
That's how I usually think of them, as well. However, sometimes, you'll see the term "inner class" used to include inner classes, local classes, and anonymous classes. In that respect, anonymous classes and local classes are just special types of inner classes.
Of course, if the class is a static member of a class, it is not an inner class at all. In that case, it is a top-level nested class.
Corey
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Just one thing Anthony...y is accessible


Ayiyi.. so it is! My mind is going...
Okay, a local class in a non-static context can access both static and non-static instance variables of the immediately enclosing class. However, a local class in a static context can only access the static variables of the enclosing class.
 
what if we put solar panels on top of the semi truck trailer? That could power 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