Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inner class basics. (Marcus Green's mock exam 2)

 
amol kat
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • 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
 
Jamal Hasanov
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
  • 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
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

Not if it's local or anonymous.
 
bani kaali
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • 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
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • 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
 
Francisco A Guimaraes
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic