Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Local Class Declarations

 
Manish Kumar
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I found the following in JLS section 14.3
"It is a compile-time error if a local class declaration contains any one of the following access modifiers: public, protected, private, or static."
But when I compiled this code it did not show any error.
class Test{
private class Test2{}

}
Please help me.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A local class is a class enclosed within a method or an initializer block.
I'd like you to read the Javaranch Naming Policy and change your publicly displayed name to comply with our unique rule. Thank you.
[ April 15, 2002: Message edited by: Valentin Crettaz ]
 
Manish Kumar
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Crettaz
But is there any difference between local classes and inner classes?
 
Jim Bedenbaugh
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Manish K:
Thanks Crettaz
But is there any difference between local classes and inner classes?

Put the inner class inside a method and try to compile it. Let us know what happens.
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manish
From the JLS section 8.1.2:
"An inner class is a nested class that is not explicitly or implicitly declared static."
And, from the JLS section 14.3:
"A local class is a nested class (�8) that is not a member of any class and that has a name. All local classes are inner classes (�8.1.2). Every local class declaration statement is immediately contained by a block."
hope that clears it up for you
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic