Declarations and Access Modifiers in Java

[siteorigin_widget class=”SiteOrigin_Widget_Headline_Widget”][/siteorigin_widget]
In a Java Program, we can contain any no. Of classes but at most one class can be declared as public. If there is any public class then the name of the Program must be the same as public class otherwise we will get compile time error. If there is no public class then any name we give for java source file.

package com.java4us;

class Test {

    public static void main(String args[]) {
        System.out.println("Welcome to Java4us.com");
    }
}
class Java4us_Testing{
    
}
class Java4us_Testing2{
    
}
  • If there is no public class in our program then we can use any name for java source file there are no restrictions.
  • If our class Test declared as public then the name of the java source file should be Test.java otherwise we will get compile time error saying “class Test is public, should be declared in a file named Test.java”.
  • If both Java4us_Testing and Java4us_Testing2 classes are declared as public and name of the file is Test.java then we will get compile time error saying “class Java4us_Testing is public, should be declared in a file named Java4us_Testing .java”.
  • It is very suggested to take only one class in our program and the name of the Program must be same as class name. This approach increases the readability and understandability of the code.
  • As a programmer, we can compile a java Program but not java class and for every class which are present in the java program, one dot class file will be created.
  • We can run a java class(.class file) but not java program(.java) whenever we are trying to run a class the corresponding class main method will be executed.
  • If the class doesn’t contain the main method then we will get runtime exception saying “NoSuchMethodError: main”.
  • If we are trying to execute a java class and if the corresponding .class file is not available then we will get runtime execution saying “NoClassDefFoundError: Test”.
 
package com.java4us;
class Test 
{
  public static void main(String[] args) 
  {
    HashMap al=new HashMap();
    System.out.println("Welcome to Java4us.com");
  }
}
CE:- Test.java:6: error: cannot find symbol
HashMap al=new HashMap();
^
symbol: class HashMap
location: class Test
  • We can resolve this problem by using the fully qualified name “java.util.ArrayList l=new java.util.ArrayList();”. But the problem with using fully qualified name every time is it increases the length of the code and reduces readability.
  • We can resolve this problem by using import statements.
package com.java4us;

import java.util.ArrayList;

class Test {
    public static void main(String args[]) {
        ArrayList al = new ArrayList();
        System.out.println("Welcome to Java4us.com");
    }
}
  • Hence whenever we are using import statement it is not required to use fully qualified names we can use short names directly. This approach decreases the length of the code and improves readability.

Types of Import Statement

 

1. Explicit class import
2. Implicit class import

1. Explicit class import:-

Example: Import java.util.ArrayList

  • This type of import is highly recommended to use because it improves readability of the code.