This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes protected in local class? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "protected in local class?" Watch "protected in local class?" New topic
Author

protected in local class?

sun par
Ranch Hand

Joined: Oct 03, 2002
Posts: 257
Hai friends,
I was reading the Java Language Specification about blocks and statements... This is what it states...
--------------------------------------------------
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
--------------------------------------------------
I ran a program like this
--------------------------------------------------
class blocktest
{
protected class sam
{
}
public static void main(String args[])
{

}
}
--------------------------------------------------
It compiled and ran fine... Can anyone explain please...
Thanks,


Sunita<br />SCJP 1.4
sun par
Ranch Hand

Joined: Oct 03, 2002
Posts: 257
class blocktest
{
public class samee
{
}
public static void main(String args[])
{
}
}
-------------------------------------------------
Even this compiles and runs... I thought that all public files should be in a file with the same name as the name of the class...
Thanks.
Eric Fletcher
Ranch Hand

Joined: Oct 26, 2000
Posts: 188
"local" means local to a block, i.e. inside a method. It is the same as the difference between local and automatic(member) variables. It makes no sense to declare an access modifier for a local variable(or class) as the variable(or class) only exists in the context of the enclosing block, hence the compiler complains.
The code you provided shows declarations of inner classes, but they are member inner classes, not local inner classes.
As for your second question, it's a compile-time error to have more than one top-level public class definition in a source file. The compiler will recognize the public class that matches the name of the source file, and all other public class definitions will cause compiler errors. in your code:

class samee is a member inner class, not a top-level class.
The following will not compile:

Try it.
On a sidenote, Java conventions recommend that all class names start with capital letters, i.e. BlockTest and Samee in your examples.
HTH.
Cheers,
E


My theory of evolution is that Darwin was adopted. - Steven Wright
sun par
Ranch Hand

Joined: Oct 03, 2002
Posts: 257
thanks E... Is much clearer now...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: protected in local class?