Java: Java Source Code Structure

Rakib Hasan
3 min readFeb 20, 2022

--

source file structure, rules of import

Java Source File Structure

  • A Java Program can contain any numbers of classes, But At most one class can declared as public,
  • if there is no public class we can use any name for java program,
  • if there is a public class, compulsory name of the program and name of the public class must be matched.
  • whenever we have compiled a java program, For every class present in java program, a separate class file will be generated
  • whenever run a class file, corresponding main method will be executed. The corresponding class doesn’t contain main method, then we will get error saying “Main method not available” If the corresponding .class file not available, then we will get error.

If we compile the above code there will be generated 4 class files (A.class, B.class, C.class).

Java Import Statement

Explicit Import
Classes are available inside the package, Explicit means direct or when we give the proper path of the java class it will call as explicit import.

Implicit Import
Implicit means indirect, When we load all the classes of the package in our java code by using (*) it will call as implicit import.

Why is it better to avoid ‘*’ in import statements in Java?

  1. ArrayList will be searched in the whole util class while in the latter it can directly go and put the reference of java.util.Arraylist in the .class file. Without the wildcard(*), the directive tells the compiler to look for one specific file in the classpath. Thus, it is suffice to say that the import directive may influence the compile time but it does not affect the runtime of the program.
  2. There are many classes and interfaces with the same name in different packages.For example:
java.lang.reflect.Array
java.sql.Array

So, if you import java.lang.reflect.* and java.sql.* you'll have a collision on the Array type, and have to fully qualify them in your code.

3. Suppose your code is something like-

import x.*;
import y.*;
...
Test t;

and class Test exists in package x. Now you check in your perfectly compiling code, and six months later, someone adds class Test to package y. you’ll have a collision on the Test type.

Hence it is better to avoid * in import package.

Other Important point of import statement

We don’t require to import for the classes & interfaces under two packages.
java.lang: String, Thread, Exception, StringBuffer
Default Package: Current working directory

Pattern class path is java.util.regex.Pattern

Package

Package is an encapsulate mechanism to group related classes/interfaces into single unit.

Advantages:

  1. Resolve Naming conflict: The biggest advantage is resolving naming conflict.
  2. Modularity: Modularity of the application will be improved.
  3. Maintainability: Maintainability of the application will be improved.
  4. Security: Package itself provide security for our component.

Universal accepted naming conversion of package: use internet domain name in reverse.

Compile Java Source file which have package:

javac -d . Test.java
Java Source Code flow

Reference: Link

--

--