• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Please solve the problem...

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
30 seconds to difuse a loaf of bread ... here, use this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic