The constructor has private access error message

I work in Java and come across an incredibly weird bug. I have a very simple class:

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 in my current project. However, when I create, package, and install on my local repo (using Maven, without creating a remote artifact repository), other projects cannot use this class correctly because they cannot create it. When I try something like:

 ClassA classA = new ClassA(); 

I get the following compilation error:

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

I decompiled .jar in my local repo to make sure the constructor is present and is publicly available. I also used the -U flag to force the update, and compilation continues to fail. What can cause this error?

+6
source share
3 answers

Perhaps you have another 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 the old version of your class.

+4
source

My only thought is that you have problems with your package. Be sure to define the package at the top of the source file for classA using the package keyword. When you call this, make sure the file is included in the include list with the include keyword. You may encounter an error because ClassA exists in some package by default, and that is what you are actually calling instead of calling your local class ClassA. The code you posted looks good, and you've already double-checked that the changes take effect in your repository.

+2
source

I have this error, where write "private", instead "public" for the class constructor;

-1
source

Source: https://habr.com/ru/post/926322/


All Articles