File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Please solve the problem... 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 » Java » Beginning Java
Bookmark "Please solve the problem..." Watch "Please solve the problem..." New topic
Author

Please solve the problem...

suresh sai
Ranch Hand

Joined: Jul 16, 2007
Posts: 62
Hi.Iam doing alott to understand the following scenario.Please help me,What is the wrong with the following code.

protected class AnotherClass
{


}


class MyClass
{
public static void main(String l[])
{
System.out.println("hello");

}

}


D:\sureshsai>javac MyClass.java
MyClass.java:1: modifier protected not allowed here
protected class AnotherClass


Thanks and regards
suresh.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by chinna suresh:
...MyClass.java:1: modifier protected not allowed here
protected class AnotherClass...

A top-level class cannot be protected or private. If you consider what these modifiers mean, it becomes clear why they can't apply to a top-level class.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
sammaiah kyatham
Ranch Hand

Joined: Aug 03, 2003
Posts: 104
Suresh,
You are using illegal specifier for the class.
Valid modifiers/specifiers are public, abstract and final.

You can use protected access specifier in inner class like below.. which is legal.

public class Test{
protected class MyProtectedTest{

}
private class MyPrivateTest{

}

}

Thanks,
Sam
suresh sai
Ranch Hand

Joined: Jul 16, 2007
Posts: 62
Hi sam,
Thanks for your help and immediate response.I understood from your good example,the modifiers that can be used for a class.But How can I make class that is protected(or private),non-inner(or non-nested) with in the file that contains "main" class or in other separate file?
Thanks in advance.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by chinna suresh:
... How can I make class that is protected(or private),non-inner(or non-nested) with in the file that contains "main" class or in other separate file? ...

Again, a top-level (non-nested) class cannot be protected or private.
Kiran Nanjuti
Greenhorn

Joined: Aug 01, 2007
Posts: 4
Hi Suresh
You can only use two access modifiers for classes(public&default) and you can use nonaccess modifiers (abstract,final,strictp).
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by KiranR:
... You can only use two access modifiers for classes(public&default)...

Note that "default" is not an access modifier. It is one term (of many) to describe the access that results from no access modifier. Other names for this include "friendly," "package," "package friendly," "package default," etc.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Please solve the problem...