Recent Posts

Wednesday, 15 June 2016

Declarations & Access Modifiers


Java Source file structure

* A java program can contain any number of classes but at most one class can be declared as the public. If there is a public class then the name of the program and name of the public class must be matched otherwise we will get compile time error.

* If there is no public class then we can use any name ass java source file name, there are no restrictions.
E.g
class A {
}
class B {
}
class C {
}
Save as
Ashok.java // Fine
Abc.java // Fine
P.java // Fine

Case 1:
     If there is no public class then we can use any name as java source file name.
E.g
A.java
B.java
C.java
Ashok.java

Case 2:
     If class B declared as public and the program name is A.java then we will get compile time error saying
"class B is public should be declared in a file named B.java

Case 3:
     If we declare both A and B classes as public & name of the program is B.java then we will get compile time error saying.
"class A is public should be declared in a file named A.java
E.g
class A {
    public static void main(String[] args) {
         System.out.println("A class main");
    }
}
class B {
   public static void main(String[] args) {
         System.out.println("B class main");
    }
}
class C {
   public static void main(String[] args) {
         System.out.println("C class main");
    }
}
class D {
}

Save as Ashok.java

javac Ashok.java

A.class
B.class
C.class
D.class

java A
A class main method

java B
B class main method

java C
C class main method

java D
R.E: NoSuchMethodError: main

java Ashok
R.E: NoClassDefFoundError: Ashok

Conclusions
1. Whenever we are compiling a java program for every class present in the program a separate .class file will be generated

2. Whenever we are executing a java class the corresponding class main method will be executed. If the class doesn't contain main method then we will get run time error saying NoSuchMethodError: main

3. If the corresponding . class file not available then we will get RuntimeException saying NoClassDefFoundError

4. It is not recommended to declare multiple classes in a single source file it is highly recommend to declare only one class per source file and name of the program we have to keep same as class name. The main advantage of this approach is readability and maintainability of the code will be improved.


Import Statement
class Test {
     public static void main(String args[]) {
          ArrayList l = new ArrayList();
     }
}
C.E: Cannot find sysmbol
Symbol: method ArrayList()
Location: class Test
* We can resolve this problem by using fully qualified name
java.util.ArrayList l = new java.utilArrayList();
* The problem with usage of fully qualified name every time increases length of the code and reduces readability

* We can resolve this problem using import statement
import java.util.ArrayList;
class Test {
     public static void main(String args[]) {
          ArrayList l = new ArrayList(); // No C.E
     }
}
* Whenever we are using import statement it is not required to use fully qualified name hence it improves readability and reduces length of the code.

Types of import statements
     There are 2 types of import statements
1. Explicit class import
E.g: import java.util.ArrayList;

* This type of import is highly recommended to use. Because it improves readability of the code

2. Implicit class import 
E.g: import java.util.*;

* It is never recommended to use this type of import because it reduces readability of the code.

Difference between #include in C and import statement in Java
* In C language #include all the specified header files will be loaded at the time of include statement only irrespective of whether we are using those header files or not. Hence this is static binding.

* But in the case of java language import statement no .class file will loaded at the time of import statement, in the next lines of the code when ever we are using a class at that time only corresponding .class file will be loaded. This type of loading is called dynamic loading or load on demand or load on fly.
import java.util.*;
import java.sql.*;
class Test {
   public static void main (String[] args) {
       Date d = new Date();
   }
}
C.E: Reference to Date is ambiguouse
Note
     Even in List case also we will get the same ambiguity problem because it is available in both util and sql packages
import java.util.Date; // Explicit class import
import java.sql.*;
class Test {
   public static void main (String[] args) {
       Date d = new Date(); // Fine
   }
}
Note
     While resolving class names compiler will always gives the precedence in the following order
1. Explicit class import
2. Classes present in the current working directory
3. Implicit class import

* The following 2 packages are not required to import because all classes and interfaces present in these 2 packages are available by default to every java program
1. java.lang package
2. default package (Current Working Directory)

* import statement is totally compile time issue if number of imports increases then compile time will be increased automatically but there is no effect on execution time.

Static import
* This concept introduced in 1.5 version

* According to SUN static import improves readability of the code, but according to world wide programming experts static imports reduces the readability of the code and creates confusion. It is not recommended to use static import if there is no specific requirement.

* Usually we can access static members by using class names, but when ever we are using static import, it is not required to use class name and we can access static members directly.
Without static import

class Test {
   public static void main(String[] args) {
      System.out.prinln(Math.sqrt(4));
      System.out.prinln(Math.random());
      System.out.prinln(Math.max(10,20));
   }
}

With static import

import static jva.lang.math.sqrt;
import static jva.lang.math.*;
class Test {
   public static void main(String[] args) {
      System.out.prinln(sqrt(4));
      System.out.prinln(random());
      System.out.prinln(max(10,20));
   }
}
About System.out.println()
class System {
    static PrintStream out;
}
System.out.println();

System - It is a class present in java.lang package
out - It is a static variable of type PrintStream present in System class
println() - It is a method present in PrintStream class
Explanation
* out is a static variable present in System class hence we can access by using class name
* But whenever we are using static import it is not required to use class name we can access out variable directly.
import static java.lang.System.out;
class Test {
   public static void main(String[] args) {
      out.println("Hello");
      out.println("Hi");
   }
}
normal import Vs static import
* We can use normal import to import classes and interfaces of a package. Whenever we are using general import it is not required to use fully qualified name and we can use short names directly.

* We can use static import to import static variables and methods of a class. When ever we are using static import then it is not required to class name to access static member we can access directly.

Next Tutorial  Packages Tutorial

Previous Tutorial  Flow Control Tutorial

No comments:

Post a Comment