• 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:

innerclasses

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hai
my exam was on 19th but due to some problems in prometric center the exam was postponed and now i will give my exam on 24sep .
now i have 3days time i am little bit confused in inner classes pls give the details if anyone have
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nested Classes
Inner Classes
An inner class is a type of nested class that is not explicitly or implicitly declared static.
Inner class types
Local inner class.
A local class is a nested class that is not a member of any class and that has a name.
Every local class declaration statement is contained by a block.
The scope of a local class declared in a block is within the rest of the block.
Local class defined in a method has access to final method variables and also to the outer class's member variables.
example
Anonymous inner class.
Anonymous inner class does not have name.
It can extend a class or implement an interface but cannot do both at the same time. (For example, an anonymous inner class cannot say extends and implements at the same time.)
The definition, construction and first use of such class is at same place.
Programmer cannot define specific constructor for anonymous class but he/she can pass arguments (to call implicit constructor of its super class.)
An anonymous class is never abstract. An anonymous class is always an inner class; it is never static. An anonymous class is always implicitly final.
Anonymous class defined in a method has access to final method variables and also to the outer class's member variables.
Non-static member class.
A member class is a inner class whose declaration is directly enclosed in another class or interface declaration.
Member inner class can be coderanch, private, protected or default/friendly.
Non-static member inner class has access to member variables of the outer class.
Non-static member inner class can be instantiated on the instance of the outer class.
example

Static nested classes.
Nested class can also be static. It has access to only static member variables of the outer class.
Static nested class may be instantiated/accessed without the instance of the outer class. It is accessed just like any other static member variable of a class.
For example, instance method accessTest() of public static nested class "Inner" can be invoked as...

new com.witscale.scjp.examples.Outer.Inner().accessTest();
hi please refer to this site for examples:http://www.witscale.com/scjp_studynotes/code_examples/chapter7
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Worth noting in the above that "inner" and "nested" aren't the same. The JLS (second edition) says that a nested class is any class declared within another class. "inner" refers to something slightly more particular.
To understand the taxonomy of nested classes I've found it helpful to remember that they can be categorized three different ways:
1. Scope
  • Member class - declared directly in the body of the class, is a full-fledged member of the class, can be static or non-static.
  • Local class - declared inside a block (method, constructor, or initializer), scoped like a local variable, cannot be static.
  • Anonymous class - an unnamed class declared by extending an existing type using special syntax which forms an expression.
  • 2. Inner - as stated in the above post. Just remember that the only things which are not inner are static member classes and interfaces (which are implicitly static).
    3. Needs-an-enclosing-instance-of-its-enclosing-class - this is decidable by inspection: can the nested class see the instance members of its enclosing class? If so, it must be instantiated with an enclosing instance of that class. Here's a list of the various nested class possibilites with a [] stating their status vis-a-vis this categorization:
  • static member class [never]
  • non-static member class [always]
  • local class [when declared in non-static context]
  • anonymous class [when declared in non-static context]

  • I will refrain from entering into any diatribe on this subject, will curb my desire to mention its unnecessary murkiness, will absolutely resist pointing out that it all could be fixed with a tiny rule change. You can find my declamations on this topic elsewhere, whether you're looking for them or not.
    Marlene uses a breakdown similar to this and it might be clearer.

    Originally posted by Marlene Miller:
    I agree, Brian. "inner class" seems like a hodge-podge collection of heterogenous kinds of classes. You cannot even say - as some people do - that they all have an enclosing instance.
    When I organize the classes in my mind, I do not include the idea of inner classes. Classes are either top-level or nested. Nested classes are member or non-member. Members are static or non-static. Non-members are local or anonymous. THEN I draw a loop around those that are inner classes (non-static member, local and anonymous).
    A few times I scanned the JLS to see exactly what the purpose of the words inner class is. What rules apply to and only to all inner classes? Well, I forget what I found, but whatever it was, it was not very useful to me.


    [ September 21, 2003: Message edited by: Steve Lovelace ]
     
    Steve Lovelace
    Ranch Hand
    Posts: 125
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Replying to my own post now (do I have bad breath?). I failed to mention what the scope is for an anonymous class. I skimmed the JLS and didn't see it mentioned. My word for it is "instantaneous scope", i.e., it is visible at the point at which it is declared, and nowhere else. A demonstration:
    [ September 21, 2003: Message edited by: Steve Lovelace ]
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic