13 How compiler search ".java" and load ".class" files in java

Whenever we are using any class definition in our method first compiler concentrate on:

1) Whether class information is available within the method or not.

For ex:  we have one class file Demo.java

Demo.java

class Demo
{
public static void main(String...r)
  {
class Student
      {
String name="Coding Atharva";
}
     Student s=new Student();
System.out.println(s.name);
}
}



So, here whenever I am writing statement Student s = new Student(); compiler will concentrate on whether the student is available within the method or not if available compiler happily exist with that code and converting that code into byte code.If not available then it is checking within the class scope.

2) Checking within the class Scope

For ex:  we have one class file Demo.java

Demo.java

class Demo{
static class Student{
String name="Coding Atharva";
}
public static void main(String...r){
Student s=new Student();
System.out.println(s.name);
}
}


If not available within the class scope then compiler is going to check within the .java file.

3) Checking within the .java file

For ex:  we have one class file Demo.java

Demo.java

class Demo{
public static void main(String...r){
Student s=new Student();
System.out.println(s.name);
}
}
class Student{
     String name="Atharva";
}




Now Suppose if we have class definition not in .java file as well then Compiler going to check in Current Working Directory.

4) Checking within the Current Working Directory :
For ex:  we have two class file Demo.java and Student.java

Student.java

class Student{
     String name="Atharva";
}

Demo.java

class Demo{
public static void main(String...r){
Student s=new Student();
System.out.println(s.name);
}
}



Case 1: .Class File existed

 Compiler check whether the 
1) .class file is existed  or .not If existed then
2) .java file existed or not If NOT then compiler using existing  .class file only.   

Case 2: .Java File existed

Compiler check whether the 
1) .class file existed or not If NOT then 
2) .java file existed or not If existed then compile it and make .class file i.e byte code. If NOT existed then compiler will give COMPILE TIME ERROR "cannot find Symbol".

Case 3: If both File existed then

If both file is exited i.e .java file and .class file then compiler is going to check which one is newly created Suppose .java file is newly created then compiler ignore the .class file and simply uses .java file and again recompile it and use the updated Byte Code.
Previous
Next Post »