This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes local nested class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "local nested class" Watch "local nested class" New topic
Author

local nested class

Lam Thai
Ranch Hand

Joined: Apr 02, 2001
Posts: 117
The following program was compiled OKed
class Outer
{
public Outer() {
}
void aMethod() {
final int x = 3;
class Inner {
}
}
}
public class LocalclassDemo {
public static void main(String[] args) {
Outer o = new Outer();
o.aMethod();
}
}
#1 But if I add 'static' modifer to the local nested class Inner like this:
static class Inner { // compile error
}
I got a compile error
#2 Or if I add this line inside non-static local nested class Inner as follows:
class Inner() {
System.out.println(x); // compile error
}
I also got a compile error
What have I missed?
Thanks in advance,
Lam
Stevie Kaligis
Ranch Hand

Joined: Feb 04, 2001
Posts: 400
Hi Lam,
Don't forget this :
1. You can not specify visibility of local variables.
in your case, you define an inner class inside aMethod(), so it's just any other member of the method, you cannot specify anything in there, not even private innerclass.
2. On a class/Interface you can only have :
a. Fields (include initialization block)
b. Methods
c. Nested classes/interface
is System.out.println(x) part of that list ???
hope this help
stevie
James Page
Greenhorn

Joined: Apr 06, 2001
Posts: 9
#1
Here class Inner is a local innner class. As you cannot have a static local variable.
#2
void print()
{
System.out.println(x);
}
should compile.

Hope it helps.
Lam Thai
Ranch Hand

Joined: Apr 02, 2001
Posts: 117
Got your points
Thanks Stevie and James
Lam Thai
Ranch Hand

Joined: Apr 02, 2001
Posts: 117
Originally posted by Lam Thai:
Got your points
Thanks Stevie and James

To be clear, I just like to reiterate what I think I got from you guys, else I might have gone astray again:
#1. Rule: Member of a method cannot have visibility. Since local nested class is a memeber of aMethod() => it should not have any visibility either. (i.e. Both 'static int x' and 'static class Inner' are unacceptable to the compiler).
#2. Rule: One can only declare Field(s), Method(s), Class(es), Interface(s) and Initialized block(s) inside Class or Interface. System.out.println() is a method call, therefore it does not belong there.
Regards,
Lam
Lam Thai
Ranch Hand

Joined: Apr 02, 2001
Posts: 117
Guess what folks...
Here is a mock question in Marcus Green mock exam #1:
Question 30:
public class MyClass1 {
public static void main(String argv[]){ }
/*Modifier at XX */ class MyInner {}
}
What modifiers would be legal at XX in the above code?
1) public
2) private
3) static
4) friend
The answer: 1,2, and 3
Per my understanding these would choke the compiler. I tried compile the code and the code does not compile. Have the rules been changed?
Thanks,
Lam

Stevie Kaligis
Ranch Hand

Joined: Feb 04, 2001
Posts: 400
read the code carefully Lam...,
Marcus code :
public class MyClass1 {
public static void main(String argv[]){ } //END of main method!
/*Modifier at XX */ class MyInner {}
}
answer is : 1,2,3
If the code :
public class MyClass1 {
public static void main(String argv[]){
/*Modifier at XX */ class MyInner {}
} //END of main method
}
answer is : Lam. None of the above (Compile error !)
NOTHING was Changed !
hope this help
stevie
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: local nested class
 
Similar Threads
Accessing Outer Class variable
doubt in InnerClass
Marcus mock exam 2 (quest. #32)
static inside inner class
Inner Class