aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inner class basics. (Marcus Green's mock exam 2) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inner class basics. (Marcus Green Watch "Inner class basics. (Marcus Green New topic
Author

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

amol kat
Greenhorn

Joined: Dec 12, 2001
Posts: 6
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

Joined: Jan 08, 2002
Posts: 411
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

Joined: Mar 22, 2002
Posts: 1055

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

Not if it's local or anonymous.
bani kaali
Ranch Hand

Joined: May 05, 2002
Posts: 42

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

Joined: Dec 20, 2001
Posts: 3271
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


SCJP Tipline, etc.
Francisco A Guimaraes
Ranch Hand

Joined: Mar 20, 2002
Posts: 182

Not if it's local or anonymous.

what�s a local inner class?
Francisco


Francisco<br />SCJP<br />please use the [code][/code] tags when showing code.Click <a href="http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=ubb_code_page" target="_blank" rel="nofollow">here</a> to see an example.
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
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

Joined: Mar 20, 2002
Posts: 182
Just one thing Anthony...y is accessible, look:

will produce this output:
Hi!
x: 7
y: 8
b: 2
Francisco
amol kat
Greenhorn

Joined: Dec 12, 2001
Posts: 6
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

Joined: Dec 20, 2001
Posts: 3271
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

Joined: Mar 22, 2002
Posts: 1055

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inner class basics. (Marcus Green's mock exam 2)