java.lang.IllegalAccessError – How to resolve Illegal Access Error

In this example we will discuss about IllegalAccessError. This is thrown if an application attempts to access or modify a field, or to call a method that it does not have access to. The definition of some class, on which the currently executing method depends, has since changed. It is normally thrown when modifying a final property of the base class, or accessing a private method or property.

The IllegalAccessError extends the IncompatibleClassChangeError, which is thrown when an incompatible class change has occurred to some class definition.

The IllegalAccessError exists since JDK 1.0.

The structure of IllegalAccessError


  • IllegalAccessError()

    Constructs an IllegalAccessError with no detail message.

  • IllegalAccessError(String s)

    Constructs an IllegalAccessError with the specified detail message.

The IllegalAccessError in Java

To make an example of IllegalAccessError, I will use the javac compiler from the command-line. This error is thrown when the classes are separately-compiled, and most of IDEs don’t allow this.

Create a Java class called BaseClass with the following source code:


public class BaseClass {
	public String message = "I am BaseClass";

Now create a new Java class called ChildClass with this source code:


public class ChildClass extends BaseClass {
	public static void main(String[] args) {
		ChildClass obj = new ChildClass();

Now to compile both of them and then run the second, I execute this at the terminal:

>_ javac BaseClass.java
>_ javac ChildClass.java

>_ java ChildClass
I am BaseClass

Everything is OK now, but when I change the BaseClass to this:


public class BaseClass {
	private String message = "I am BaseClass";

and compile and rerun the ChildClass, this error is thrown:

Exception in thread "main" java.lang.IllegalAccessError: tried to access field BaseClass.message from class ChildClass
        at ChildClass.main(ChildClass.java:5)

The same would happen if we try to change the message. Now rewrite the ChildClass to look like this:


public class ChildClass extends BaseClass {
	public static void main(String[] args) {
		ChildClass obj = new ChildClass();
		obj.message += " Am I not?";

Compile it (make sure that message is public) and then make BaseClass look like this:


public class BaseClass {
	public final String message = "I am BaseClass";

Recompile the BaseClass and rerun ChildClass. You will see

Exception in thread "main" java.lang.IllegalAccessError
        at ChildClass.main(ChildClass.java:5)

More about the IllegalAccessError in Java

IllegalAccessError is thrown when the programmer:

  • Accesses a private field
  • Modifies a final field
  • Calls a private method

Normally, the compiler would fail to compile the class when one of these occurs, but the IllegalAccessError is thrown when the base class is recompiled without changing the derived (i.e. child) class.

How to deal with IllegalAccessError

When you face this error, you need to change the derived class to fit the characteristics of the base class. This may be using getters and setters instead of calling directly fields, which may be private, etc.

Download Code

You can download the full source code of this example here : IllegalAccessErrorExample

Want to know how to develop your skillset to become a Java Rockstar?

Join our newsletter to start rocking!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions


Aldo Ziflaj

Aldo is a student of Computer Engineering and a programming addict. He spares his free time coding, whether mobile, web, or desktop programming. He is also one of the co-founders of Things Lab.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button