• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Inner class

 
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 coderanch.
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
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to both of you.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was reading from Moughal book
web page
Refer to table 7.1. The examples follows.
 
Lavanya Sunkara
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reference, Barkat.
The book is really good and very deep.
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic