Compilation error:-
When the compiler try
to compile a java program, it will try to create a proper environment for JVM
to execute also.Even though, if it is not possible to create a proper
environment, the compiler give us an error is known as compilation error or compile time error.
E.g.:-
class X {
int
x;
static int y;
void methodX
() {
System.out.println ("I am
in methodX of class X");
} }
|
Note:- Here the x.java
is saved, but not compile.
class Y {
int i;
static int y;
void methodY () {
System.out.println
("I am in methodY of class Y");
}
public
static void main (String [] args) {
X x1=new X ();
x1.methodX (); //this will
give you a compilation error
} }
|
Save the class Y as
Y.java, when we compile this Y.java, the output is compilation error, because
the X.java is not compiled before the compilation of Y.java.
Runtime error:-
Note:-
After compiler will create
proper environment for JVM to execute, JVM will face some problems in some
situations while executing a program. Then it will generate an error known as “Dynamic error or Runtime error”.
class X {
int x;
static int y;
void method () {
System.out.println
("I am in methodX of class X");
} }
class Y {
int i;
X x = null;
void methodY () {
System.out.println
("I am in methodY of class Y");
}
public
static void main (String [] args) {
Y y1 = new Y ();
y1.x.methodX ();
// Dynamic error will come
//
Null pointer Exception
// we
cannot operate on null objects.
//
that means we cannot access non-static
//contents
form
//
null references or null objects.
} }
|
Responsibilities
of a compiler:-
·
Converts the sources code (.java) into byte
code (.class).
·
While conversion, it checks for proper environment
available for JVM to execute the program.
1. If available, then it will generate a byte code creates a .class
file.
2.
If not available, the compiler
will generate a compilation error.
class A {
int t;
void funA() {
System.out.println
("inside funA ()");
} }
class StaticRef {
int x;
static int j;
static A a1 = new A
();
public
static void main (String args []) {
staticRef.a1.funA
();
} }
|
Explanation:-
staticRef.i=staticRef.i+100;
When this statement is
executed ‘i’ will be accessed by the JVM from the context of staticRef.class
file.
staticRef.a1.funA ()
When this statement is
executed ‘a1’ will be accessed from context of staticRef.class file. Since ‘a1’ is referring an instances
of (object) class A.
We can access the funA()
from A class instance.
This funA () will be loaded
into the RAM, executed and it will be deleted from RAM.
But context of the
staticRef.class will be deleted only when JVM completes the execution of main
().
In System.out.println ()
statement “out” is a static variable inside “System” class, so we can directly
access as System.out.
“Out” is representing one
more class object and println is a method inside of that object.
No comments:
Post a Comment