|
-
Nov 10th, 2001, 12:53 PM
#1
Thread Starter
Hyperactive Member
What are inner classes?
Just like the title suggests, I want to know what inner classes are. I've heard them mentioned (I'm pretty sure ) but haven't seen anything on them? Do they even exist?
"There are only two things that are infinite. The universe and human stupidity... and the universe I'm not sure about." - Einstein
If you are programming in Java use www.NetBeans.org
-
Nov 10th, 2001, 01:00 PM
#2
Hyperactive Member
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.
-
Nov 10th, 2001, 01:02 PM
#3
Thread Starter
Hyperactive Member
<whew>at least i know i'm not crazy... or at least not as bad but can you explain that a bit moer, cuz i thinki i got it, and could anyone give an example?
"There are only two things that are infinite. The universe and human stupidity... and the universe I'm not sure about." - Einstein
If you are programming in Java use www.NetBeans.org
-
Nov 10th, 2001, 07:16 PM
#4
Dazed Member
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.
-
Nov 12th, 2001, 11:45 AM
#5
Thread Starter
Hyperactive Member
i think i'm getting it... could someone give a code example to see what this looks like?
"There are only two things that are infinite. The universe and human stupidity... and the universe I'm not sure about." - Einstein
If you are programming in Java use www.NetBeans.org
-
Nov 12th, 2001, 12:19 PM
#6
Dazed Member
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.
}
}
}
-
Nov 12th, 2001, 12:29 PM
#7
Dazed Member
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|