Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"constructor has private access" error message

I'm working in Java and have come across an incredibly odd error. I have a very basic class as follows:

public class ClassA{
   private static Logger log = Logger.getLogger(ClassA.class.getName());
   private boolean trace;

   public ClassA(){
      trace = log.isTraceEnabled();
   }

   public void doSomething(){
      //does stuff
   }
}

I can use this class just fine within my current project. However, when I build, package, and install to my local repo (using Maven, no remote artifact repo set up), other projects cannot properly use this class because they cannot instantiate it. When I try anything like:

ClassA classA = new ClassA();

I get the following compilation error:

ClassA() has private access in [package].ClassA

I've decompiled the .jar in my local repo to ensure the constructor is present and is public - it is. I've also used the -U flag to force updates and the compilation continues to fail. What could be causing this error?

like image 697
josh-cain Avatar asked Sep 26 '12 15:09

josh-cain


People also ask

Does constructor have private access?

A private constructor in Java is used in restricting object creation. It is a special instance constructor used in static member-only classes. If a constructor is declared as private, then its objects are only accessible from within the declared class. You cannot access its objects from outside the constructor class.

Can constructor use private access modifier?

Like methods, constructors can have any of the access modifiers: public, protected, private, or none (often called package or friendly). Unlike methods, constructors can take only access modifiers. Therefore, constructors cannot be abstract , final , native , static , or synchronized .

Can a constructor be private or protected?

Access specifiers/modifiers allowed with constructors Modifiers public, protected and, private are allowed with constructors. We can use a private constructor in a Java while creating a singleton class.

Can Java have private constructor?

Java allows us to declare a constructor as private. We can declare a constructor private by using the private access specifier. Note that if a constructor is declared private, we are not able to create an object of the class. Instead, we can use this private constructor in Singleton Design Pattern.


2 Answers

Maybe you have some other ClassA.class file somewhere in the classpath. Check all the jars used by the project that cannot call the constructor: one of them should contain an old version of your class.

like image 180
Pino Avatar answered Sep 18 '22 13:09

Pino


My only thought is that you have a problem with your package. Make sure to define the package at the top of the source file for classA using the package keyword. When you call it ensure that the file is in include list with the include keyword. You could be running into the error because ClassA exists in some default package and that is what you are actually calling instead of calling your locally made ClassA class. The code you posted looks fine and you have already double checked to ensure the changes have taken effect in your repository.

like image 39
Scott Avatar answered Sep 16 '22 13:09

Scott