Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is access checked at compile time or run time? [duplicate]

Tags:

java

oop

Possible Duplicate:
JVM/Java, are method accessibility rules enforced at runtime?

When using a class that has both public and private fields and members, is the code checked for access violations (e.g. trying to access a private field from an outside class) at compiled time or run time? Or both?

like image 355
corazza Avatar asked Dec 08 '25 22:12

corazza


1 Answers

Both. If you try to compile code which tries to access inaccessible object or method, you'll get compile-time error:

field has private access in package.Class

Also, when your class tries to access some field at runtime, JVM checks the access:

Exception in thread "main" java.lang.IllegalAccessError: tried to access field package.Class.field from class YourClass
like image 67
tcb Avatar answered Dec 11 '25 12:12

tcb