Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Local Inner Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Local Inner Class" Watch "Local Inner Class" New topic
Author

Local Inner Class

sreelakshmi sarma
Ranch Hand

Joined: Mar 02, 2000
Posts: 130
I read from somewhere that a class declared inside a method(local inner class) can have final or abstract modifiers. I am sure of final but i don't understand how it can be made abstract. I tried it and it's working fine. Could some body please explain me this. Any thing inside method can only get final modifier right???
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Sree,
A local class inside a method can be abstract OR final. There is nothing wrong in that. But we have to find out what is the use of declaring a local class as abstract and extending the class within the method itself. ANy idea ?
Alkesh ,
We really appreciate your participation here even after you got your SCJP2 certification. Have you finished reading the WeakHashMap part?
A small correction to what you said:
The class which is defined inside the scope of a method can have access ONLY to the final variables of the enclosing method AND ALL of the variables of the enclosing class.
This is not quite true. A class which is defined inside a method can hava access to all final vars inside the same method where it is declared, and all of the variables of the enclosing class WHICH ARE AVAILABLE to the ENCLOSING METHOD
Because an inner class which is defined inside a static method can have access to ONLY STATIC vars of the enclosing class. I am sure you know this. Any way this is the info that all should be aware of. In other words
<pre>

class Outer {
static int staticInt =10;
int instInt = 20;

static void m1() {
class LocalClassInsideStaticMethod {
void m2() {
System.out.println(staticInt); //This is Ok
System.out.println(instInt); //This is NOT Ok
}
}
}
}

</pre>
regds
maha anna
sreelakshmi sarma
Ranch Hand

Joined: Mar 02, 2000
Posts: 130
Thanks Maha & Alkesh.
Maha i have no idea why we declare a class inside a method as abstract. Could you please explain me.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Quote ... from Maha Anna's post above :
"...same method where it is declared, and all of the variables of the enclosing class WHICH ARE AVAILABLE to the ENCLOSING METHOD"
In my openion, this should be
"...WHICH ARE AVAILABLE to the ENCLOSING METHOD
and are declared FINAL."
Since there may be a lot more variables "which are available in
the enclosing method". However, the inner classes declared
inside a method can only access FINAL variables (either
declared as local variables in the method or passed to the
method as arguments).
Correct me if I am wrong.
Regds.
- satya
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Satya,
What I said was indeed correct. It can access all local-final var and final arg and all var from the enclosing class which are available to the method(both final anf non-final from enclosing class). Here is the evidance. Take a deep breath and look into it. I welcome others also to do some research on this foll code. Since it will DEFINITELY help you understand what is available and what is not available to local inner classes inside a method. I want this code to be of some use for others. I had done the hard-part. Now it's time for you all to play with it.
regds
maha anna

The output is
-------------
var 1 = 1
var 1 = 4
var 1 = 8
var 1 = 9
var 1 = 10
var 1 = 12
var 1 = 14
var 1 = 16
var 1 = 17
var 1 = 18
var 1 = 19
var 1 = 20
var 1 = 21
var 1 = 22
var 1 = 23
var 1 = 24
var 1 = 25

<pre>
class Test {
static int var_1 =1; //outer static var
int var_2 = 2; //outer instance var
final int var_3 =3; //outer instance final var
static final int var_4 =4; //outer class level final var
public int var_5 =5;//outer instance public var
protected int var_6 = 6; //outer instance protected var
private int var_7 = 7; //outer instance private var
static public int var_8 =8;//outer static public var
static protected int var_9 = 9; //outer static protected var
static private int var_10 = 10; //outer static private var

static void m1(int var_11, final int var_12) { // non-final arg, final arg
int var_13=13; //enclosing method's local non-final var
final int var_14 = 14; // enclosing method's local final var
class localClass {
//static int var_15 =10; //local class member can't be static
int var_16=16;
final int var_17=17;
static final int var_18=18; //This is allowed
public int var_19=19;
protected int var_20 = 20;
private int var_21 = 21;
void printAllVariables(int var_22, final int var_23) { //inner class's instance method/final,non-final arg
int var_24=24; //local non-final var
final int var_25=25; //local final var
//Let us check... :-)
System.out.println("var 1 = "+var_1);
//System.out.println("var 1 = "+var_2); //Outer instance var NOT OK
//System.out.println("var 1 = "+var_3); //outer instance final NOT OK
System.out.println("var 1 = "+var_4);
//System.out.println("var 1 = "+var_5);//outer instance public NOT OK
//System.out.println("var 1 = "+var_6);//outer instance protecd NOT OK
//System.out.println("var 1 = "+var_7);//outer instance private NOT OK
System.out.println("var 1 = "+var_8);
System.out.println("var 1 = "+var_9);
System.out.println("var 1 = "+var_10);
//System.out.println("var 1 = "+var_11);//outer enclosing method nonfinal arg NOT OK
System.out.println("var 1 = "+var_12);
//System.out.println("var 1 = "+var_13);//outer enclosing method nonfinal local var NOT OK
System.out.println("var 1 = "+var_14);
//System.out.println("var 1 = "+var_15);
System.out.println("var 1 = "+var_16);
System.out.println("var 1 = "+var_17);
System.out.println("var 1 = "+var_18);
System.out.println("var 1 = "+var_19);
System.out.println("var 1 = "+var_20);
System.out.println("var 1 = "+var_21);
System.out.println("var 1 = "+var_22);
System.out.println("var 1 = "+var_23);
System.out.println("var 1 = "+var_24);
System.out.println("var 1 = "+var_25);
}//end of printAllVariables()
}//end of localclass declaration
new localClass().printAllVariables(22,23);
}//end of m1()
public static void main(String[] args) {
new Test().m1(11,12);
}
}

</pre>

[This message has been edited by maha anna (edited April 17, 2000).]
Eric Barnhill
Rancher

Joined: Feb 25, 2000
Posts: 233

I have no good guess on the function of an abstract local inner class. The suspense is killing me!
Also, with regard to the class Outer earlier in the post, is there any way to instantiate the class LocalClassInsideStaticMethod, from an outside class? I tried a few ways but it seems not do-able.
Eric
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Eric & Sree- I can write an example of an abstract local class, but I can't really come up with any example where it would actually be useful for anything. An abstract class can't really be useful unless you're going to create at least two other classes that implement it - if there's just one, then why bother putting some functionality in the abstract class and some in the concrete subclass? Just put it all in the one class. And the abstract local class is only in scope inside the method it's defined in - so the only place to implement other classes which extend it, is inside the same method. So we're talking about at least three separate local classes inside the same method, in addition to the code that actually uses the classes. Ugh! That's going to be an unreadable nightmare. Methods should be kept short for readability. All those local classes would work much better as top-level classes or member classes, defined somewhere outside the method where they won't create such confusion. If the classes need access to some local variables, then create methods that allow the variable value to be passed.
Actually, I feel much the same about most local classes. Anonymous classes can be OK if they're short, but named local classes take up too much space inside a method. You can use them, but in most cases you probably shouldn't.
Anyway, abstract classes are legal, and local classes are legal, and so abstract local classes are legal because there's no fundamental reason why they should be impossible - they're just undesireable, in my opinion.
As for Eric's second question- no. I don't believe it's ever possible to instantiate any local class from outside the method that defines it, much less outside the class that defines it.

"I'm not back." - Bill Harding, Twister
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
See, I pulled Jim to come here. Actually I was also wondering what could be the use of method-level-abstract inner classes. I waited for Jim.
regds
maha anna
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944

Maha:
Thanks for your code. That did clear some mis-concept
(I never had a concept ) that I had about Inner classes
being able to access "all variables" which the method
has access to.
And one other thing ...
keep positing such good stuff
Regds
- satya
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Satya,
I am glad atleast one person used this code.
regds
maha anna
PalaniM
Greenhorn

Joined: May 28, 2000
Posts: 2
Maha,
I got this error when I compiled your code with jdk1.1.6. Please take a look.
Error: Variable var_18 can't be static in Test. 1$localClass. Only members of interfaces and top-level classes can be static.
With jdk1.2.2 abd jdk1.3 it compiled with no error. I am sorry for the confusion. You can remove this message. Thank you.
[This message has been edited by PalaniM (edited May 30, 2000).]


Jay
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Palani,
I once again compiled and checked. It compiles fine. I am using JDK1.2.2 on WIN98. Which compiler are you using?. Is this the only error you are getting?
regds
maha anna
PalaniM
Greenhorn

Joined: May 28, 2000
Posts: 2
Maha,
I have typed my reply in my previous message. Thanks.
imran anwar
Greenhorn

Joined: May 06, 2002
Posts: 12
so.. did anyone in any other thread figure out what a local abstract inner class can be used for?
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Please sree and satya5 read our name policy and change your displayed name accordingly.
Thanks.


SCJP2. Please Indent your code using UBB Code
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

Originally posted by Jose Botella:
Please sree and satya5 read our name policy and change your displayed name accordingly.
Thanks.

--- good effort Jose -- except those two posts are dated April and May of 2000 :roll: -- don't think they'll be changing their names anytime soon...
Robert Alkire
Greenhorn

Joined: May 13, 2002
Posts: 2
It would be strange to provide an abstract inner class within a method; however, placing a class within a method probably makes it invisible even to reflection.
FYI. You can also declare a private class within an interface (except on HP)... Good for enums.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120

good effort Jose -- except those two posts are dated April and May of 2000 -- don't think they'll be changing their names anytime soon...

oh come on good will is what counts. I tried, but the dates are just nuances :roll:
No, it is not possible to declare a private class within an interface. All the memebers in an interface are public.
 
wood burning stoves
 
subject: Local Inner Class
 
Similar Threads
INNER CLASSES
Local class is a inner class?
local inner classes
accessibility modifiers for classes
inner classes