
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
Java - IllegalAccessException
The Java IllegalAccessException is thrown when an application tries to reflectively create an instance (abstract class, interface, array class, primitive type, or void) that is not accessible. The IllegalAccessException is a checked exception that is thrown when you try to access a field, method, or constructor that is not accessible.
Following is the reason when JVM throws an IllegalAccessException in Java:
- When the user tries to access a field, method, or constructor that is not accessible, JVM throws an IllegalAccessException.
Constructors of IllegalAccessException
There are two constructors of IllegalAccessException class:
- IllegalAccessException(): This constructor is used to create an IllegalAccessException object without any message.
- IllegalAccessException(String message): This constructor is used to create an IllegalAccessException object with a message.
Methods of IllegalAccessException
There are some methods of IllegalAccessException class:
Method | Description |
---|---|
getMessage() | It is used to return the message of the exception. |
toString() | It is used to return the detail message string of the exception. |
printStackTrace() | It is used to print the stack trace of the exception. |
Example of IllegalAccessException
In this example, we are trying to access a field that is not accessible, so JVM will throw an IllegalAccessException.
import java.lang.reflect.Field; public class IllegalAccessExceptionExample { public static void main(String[] args) { try { Class<?> cls = Class.forName("java.lang.String"); Field field = cls.getDeclaredField("value"); // Try to access a private field without setting it accessible System.out.println(field.get("Hello")); // This will throw IllegalAccessException } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (NoSuchFieldException e) { e.printStackTrace(); } } }
Output
Following is the output of the above code:
IllegalAccessExceptionExample.java:9: error: unreported exception IllegalAccessException; must be caught or declared to be thrown System.out.println(field.get("Hello"));
Handling IllegalAccessException
In this example, we are trying to access a field that is not accessible, so JVM will throw an IllegalAccessException. We are handling this exception using try-catch block.
import java.lang.reflect.Field; public class IllegalAccessExceptionExample { public static void main(String[] args) { try { Class<?> cls = Class.forName("java.lang.String"); Field field = cls.getDeclaredField("value"); field.setAccessible(false); System.out.println(field.get("Hello")); } catch(IllegalAccessException e) { System.out.println("Can't access a field that is not accessible"); } catch(ClassNotFoundException e) { e.printStackTrace(); } catch(NoSuchFieldException e) { e.printStackTrace(); } } }
Output
Following is the output of the above code:
Can't access a field that is not accessible
How to avoid IllegalAccessException?
There are some ways to avoid IllegalAccessException in Java:
- Always check the accessibility of the field, method, or constructor before accessing it.
- Always use the setAccessible() method to set the accessibility of the field, method, or constructor.
- Always use the getModifiers() method to check the accessibility of the field, method, or constructor.
By following the above ways, you can avoid IllegalAccessException in Java.