File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inner class 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" Watch "Inner class" New topic
Author

Inner class

suresh kamsa
Ranch Hand

Joined: Jul 30, 2001
Posts: 149
class Outer{
class Inner extends HasStatic{
static final x = 3; static int y = 4; //line1// compile-time error, an inner class
}
static class NestedButNotInner{ // line2
static int z = 5; //line3
}
}
1.What is the difference between Inner class and Nested class.
2.Accroding to books I refered Inner/netsted class is one which is defined inside anonther class.
3. line2 is not inner class?
4. If I define class starting with static initializer inside another class that will not become inner class? Will it become netsted class?
5. Why line3 will not give compile-time error?
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
class Outer{
class Inner extends HasStatic{
static final x = 3; static int y = 4; //line1 // compile-time error, an inner class
}
static class NestedButNotInner{ // line2
static int z = 5; //line3
}
}

1.What is the difference between Inner class and Nested class.

There are for type of nested classes:
a. Top level nested classes or interfaces (always static)
b. Non static inner class (always non-static)
c. Local class (can be static or non-static)
d. Anonymous class (can be static or non-static)
The last three are collectively called "inner classes".

3. line2 is not inner class?

Line 2 is top level nested class because it is static but not inner by definition.

4. If I define class starting with static initializer inside another class that will not become inner class? Will it become netsted class?

Yes.

5. Why line3 will not give compile-time error?

Top level nested class can have static members. Non-static inner class can not.
Hope this helps.
Barkat
Vin Kris
Ranch Hand

Joined: Jun 17, 2002
Posts: 154
Hi Suresh, I suppose there is another class called HasStatic defined. neways, i'll try to sum up.... (though lines 3 & 4 become answers if you remove the '?')
A class enclosed by another class is called an inner class and can be qualified as private, protected or public.
But, If the class is qualified with the keyword 'static', then it is not an inner class - it is a top level nested class.
static member variables can only be declared in top-level classes. Therefore, line 2 is correct but line 1 gives error.
For line 1 to compile, you have 2 choices -
1) make class Inner too static.
2) make the variable y also static final.
The only other point to note is that inner classes may inherit static variables.
suresh kamsa
Ranch Hand

Joined: Jul 30, 2001
Posts: 149
Thanks to both of you.
Lavanya Sunkara
Greenhorn

Joined: Aug 23, 2002
Posts: 4
Originally posted by Barkat Mardhani:
There are for type of nested classes:
a. Top level nested classes or interfaces (always static)
b. Non static inner class (always non-static)
c. Local class (can be static or non-static)
d. Anonymous class (can be static or non-static)
The last three are collectively called "inner classes".
Lavanya Sunkara
Greenhorn

Joined: Aug 23, 2002
Posts: 4
I am sorry. Posted before asking the question.....
Originally posted by Barkat Mardhani:
There are four type of nested classes:
a. Top level nested classes or interfaces (always static)
b. Non static inner class (always non-static)
c. Local class (can be static or non-static)
d. Anonymous class (can be static or non-static)
The last three are collectively called "inner classes".

Hi Barkat,
your points a and b are clear.
Is Local class a class declared in a method? Please correct me if I am wrong.....
Can an anonymous class be either static or non-static?
Can u please give me an example for an anonymous static class?
Thanks in advance
Lavanya.
Lavanya Sunkara
Greenhorn

Joined: Aug 23, 2002
Posts: 4
And I just came across a stmt about anonymous class in JLS as

An anonymous class is always an inner class (�8.1.2); it is never static (�8.1.1, �8.5.2).

Thanks,
Lavanya
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
I was reading from Moughal book
web page
Refer to table 7.1. The examples follows.
Lavanya Sunkara
Greenhorn

Joined: Aug 23, 2002
Posts: 4
Thanks for the reference, Barkat.
The book is really good and very deep.
pree sree
Ranch Hand

Joined: Jul 27, 2002
Posts: 34
there was a similar query abt inner classes referring to Marcus Green's mock exam,in which it was answered that static can be used with inner classes.ie, there are 4 types of inner classes.when static is used it is referred to as top level nested class.so is it nested class or inner class which has the 4 types?(i am sorry ,but i am a bit confused)
[ September 06, 2002: Message edited by: pree sree ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inner class