Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Please solve the problem...

 
suresh sai
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
sammaiah kyatham
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic