"constructor has private access" error message
Asked Answered
T

5

6

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?

Telltale answered 26/9, 2012 at 15:5 Comment(6)
What is the package definition ?Accordion
Can you post the javap output for the class?Bel
ClassA and it's calling class (we'll say ClassB) are in two different packages at the same level - so it would be my.package.one.ClassA and my.package.two.ClassBTelltale
Is that "Public" (with a capital "P") actually in your classfile, or is it an artifact of copy-paste?Alidis
This is a sanitized version of my class. The names have been changed to protect the innocent so a few typos snuck in there...Telltale
Both answers seems good enough. What I advise to do is to change your ClassA name to another one, and see what happens. Maybe a conflict is masking your problem and avoiding conflict it becomes evident.Dhar
M
4

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.

Minda answered 26/9, 2012 at 15:15 Comment(1)
I got bit by this today. In my case, my errant source file was in a directory that contradicted its package declaration.Bilabiate
P
2

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.

Polloch answered 26/9, 2012 at 15:13 Comment(2)
I just checked to verify and I explicitly import my.package.one.ClassA when attempting to instantiate it.Telltale
For completeness, change ClassA classA = new ClassA(); to ClassA classA= my.package.one.ClassA();Polloch
A
2

//for those with Kotlin-Java mixed projects:

If the said file (With constructor) is in Kotlin and is being used in Java:

Instead of A a = new A(); //which causes the said error

Use A.INSTANCE. …

Application answered 15/6, 2018 at 2:5 Comment(0)
B
0

I have this Error with Desktop is not a "class". i have this Error while Desktop is a "enum". The very strange error occurred when generating the class with Intellij. certainly when generating the class automatically I clicked on the enum label instead of class

The very strange error occurred when generating the class with Intellij. certainly when generating the class automatically I clicked on the enum label instead of class [![enter image description here][1]][1]

It's stupid but it's like that, it can happen to anyone

In addition, Intellij offers to create an enumeration in first order, so if you are used to pressing the Enter button, you fall directly into the error [1]: https://i.sstatic.net/pzb6bBfg.png

Berget answered 26/7, 2024 at 13:20 Comment(0)
P
-1

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

Prurigo answered 26/2, 2014 at 9:59 Comment(1)
Um, why is that an answer?Hypercorrection

© 2022 - 2025 — McMap. All rights reserved.