Thursday, May 19, 2011

Published 5/19/2011 by with 4 comments

modifier private not allowed here

Java error "modifier private not allowed here" can occur for any of the following reasons:

1. An outer class declared as private like below:
private class OuterClass
{
    //...
}
An outer class only be public or package private not private or protected. So you can write an outer class as below:
public class OuterClass
{
   //...
}
or
class OuterClass
{
   //...
}
This is true for an interface also. An interface can only be public or package not private or protected.


2. An interface method declared as private.

public interface TestInterface
{
   private void method(); // wrong as the interface method is private
}

An interface can contain only public or package private method.

The correct code of the above is:

public interface TestInterface
{
   public void method();
}
or
public interface TestInterface
{
   void method(); // interface method declared as package private
}
    email this       edit

4 comments:

  1. An interface can contain only public methods (or private static final fields). Since the interface specifies a set of exposed behaviors, all methods are implicitly public abstract. It really doesn't add a lot of value to explicitly specify these modifiers.

    ReplyDelete
  2. Interface fields(member variables) are public static final

    ReplyDelete
  3. I have interface with package private method, when I am implementing it in a class its giving me
    "attempting to assign weaker access privileges; was public" compiler error
    I am using JDK 1.6
    java version "1.6.0_45"
    Java(TM) SE Runtime Environment (build 1.6.0_45-b06)
    Java HotSpot(TM) 64-Bit Server VM (build 20.45-b01, mixed mode)

    ReplyDelete
  4. Please have a look at http://java-error-messages.blogspot.com/2011/05/attempting-to-assign-weaker-access.html

    ReplyDelete