Just like the title suggests, I want to know what inner classes are. I've heard them mentioned (I'm pretty sure :rolleyes: ) but haven't seen anything on them? Do they even exist?
Printable View
Just like the title suggests, I want to know what inner classes are. I've heard them mentioned (I'm pretty sure :rolleyes: ) but haven't seen anything on them? Do they even exist?
Yeah Inner classes exist. They're often referred to as local nested classes. You can only create objects in an inner class locally. It's useful when the computation in a method requires the use of a specialised calss that is not required or used elsewhere. An inner class can refer to variables declared in the method in which the definition appears, but only if they are final.
<whew>at least i know i'm not crazy... or at least not as bad :D but can you explain that a bit moer, cuz i thinki i got it, and could anyone give an example?
Without getting to in-depth. There are a host of inner classes that can be declared in Java.
1.) static Local inner classes (sometimes refered to as top level nested classes).
2.) non-static inner classes
3.) Local class (non-static)
4.) Local class (static)
5.) Anonymous classes (non-static)
6.) Anonymous classes (static)
Working with inner classes can be quite confusing sometimes becuase based on the way an innerclass is defined access of certain members of it's enclosing context might me forbidden.
Here's a quick breakdown of what some innerclasses can access
and declare.
static local inner classes can declare bolth static and non-static members but can only access static members within it's enclosing context. They can use any accessability modifiers public private ect...
non-static inner classes can declare only non-static members but can access all members.
local classes (non-static) can declare only nonstatic members. can access all members + local final variables.
local classes (static) can declare only non-static members and access static members of it's enclosing context + local final variables.
Anonymous class (non-static) can declare only non-static members but can access all members in it's enclosing context + plus local final variables.
Anonymous class (static) can declare only non-static members and access static members in it's enclosing context.
:confused: i think i'm getting it... could someone give a code example to see what this looks like?
Here's an example of a top level nested class.
You'll notice that the nested top level class
can declare bolth static and non-static members
but only access static members of it's enclosing context.
Code:public class example1{
public static void main(String[] args){
toplevelnestedclass tlnc = new toplevelnestedclass();
}
static int i = 100;
long l = 2586;
static class toplevelnestedclass{
static double d = 2.0; // can declare bolth static and non-static
float f = 1.0f;
toplevelnestedclass(){
System.out.println(" The value of i is " + i);
System.out.println(" The value of l is " + l); // non-static variable l cannot be refrenced from a static context.
}
}
}
I would often get confused when trying
to remember what innerclass could declare
and access what so i now just remember that
the only class that can declare bolth static
and non-static members is a top level nested
class. Everything else can only declare non-static
members.
Now as to what a nested class can access
i just remember that
if the class is static or declared within a static
context like a static method then it can only
access static members of it's enclosing context
plus local final variables. If the class is non-static
or declared within a non-static context then it can
access all members and local final variables of
it's enclosing context.