Java Source File Structure
A Java program can contain any number of classes but at most one class can be declare as public. name of java program and public class must be same otherwise we will get compiler time error, if there is no public class then we can we use any name as java source file name there is no restriction.
Note : It is highly recommended to take only one class per source file & name of the file and class name must be match, this approach improves the readability of the code.
import Statement - we can resolve this issue by using fully qualified name, the problem with fully qualified name is that every time increases length of code and reduce the readability. we can resolve this problem by using import Statement, whenever we are using import statement it is not required to use fully qualified name hence it improves the readability and reduce the length of code.
Types of import Statement
import java.util.AL;
This type of import is highly recommended to use because it improves the readability of the code
import java.util.*;
It reduces the readability of the code
Difference between #include & import Statement
In C language #include all the specified header files will be loaded at the time of include statement only respectively, whether we are using those header files or not, hence this is static loading. but in the case of Java import statement, no .class file will be load at the time of import statement, in the next line of code whenever we are using a class at that time only the corresponding .class file will be load, this type of loading is called dynamic loading.
Package - It is an encapsulation mechanism to group related class and interface into a single module. the main purpose of package are
- To resolve naming conflicts
- To provide security to the class & Interface, so that outside person can't access directly.
- It improves the modularity of the application
Access modifiers are keywords in object-oriented languages that set the accessibility of classes, methods, and other members. Access modifiers are a specific part of programming language syntax used to facilitate the encapsulation of components. Access modifiers specifies access level meaning who can access the entity to which it is applied. Access modifier can be used to control the access of classes and fields and methods of the class.
The only applicable modifiers for top level classes are - public, <default>, final, abstract, Strictfp
For Inner class these following modifiers are allowed - public, <default>, final, abstract, Strictfp, private, protected, static
access specifiers vs access modifiers
In other language like C, C++ public, private, protected & default are considered as access specifiers and all the remaining like final, static are considered as access modifiers, but in Java no such type of division, all are considered as access modifiers
public - If a class is declared as the public then we can access that class from anywhere
default - Ia a class is declared as default then we can access that class within the same package
final - is the modifier applicable for classes, methods and variables,
- If the method is declared as final then we are not supposed to override the method in the child class.
- If a class is declared as final then we are not supposed to create child class
- Every method present inside a final class is always final by default but every variable present in final class need to be final
- The main advantage of using final is we can achieve security as no one is allowed to change implementation, but the main disadvantage of using final is we are missing key benefits of OOPs concept inheritance and polymorphism. hence if there is no specific requirement then it is not recommended to use final keyword.
abstract - applicable only for class and method
abstract method - even though we don't know about implementation still we can declare a method with abstract modifier. i.e abstract method can have only declaration but not implementation. syntax public abstract void methodName();
child class is responsible to provide implementation for parent class abstract method. abstrct modifier never talks about implementation.
abstract class - For any java class if we dont want instantiation the we have to declare as abstract class. for abstract class instantiation (creation of object) is not possible.
abstract class vs abstract method -
If a class contains at least one abstract method then compulsory that class should be declared as abstract otherwise we will get compile time error, because the implementation is not complete and hence we can't create an object. even though class doesn't contain any abstract method still we can declare the class as abstract i.e abstract class can contain zero number of abstract method
HttpServlet this class doesn't contain any abstract method but still it is declared as abstract
- abstract method we have to override in child class to provide implementation, where as final method can't be overridden. hence abstract and final combination is illegal combination for method.
- for abstract class we should create child class to provide proper implementation but for final class we can't create child class.
- final class can't have abstract method where as abstract class can contain final method
strictfp (strictfloatingpoint) - is the modifier applicable for method and class
- if the method declares as strictfp all floating point calculation in that method has to follow IEEE 754 standard so that we will get platform independent result.
- strictfp method always talks about implementation.
- If a class declares as strictfp then every concreate (method having complete definition) method in that class has to follow IEEE 754 standard
- abstract strictfp combination is legal for class but illegal for methods
- If we declare a member as public then we can access that member from anywhere but corresponding class should be visible public, i.e before checking member visibility we have to check class visibility.
- If a member declares as default then we can access that member only within current package, hence default access also known as package level access.
- if a member declares as private then we can access that member only within the current class.
- If a member declares as protected then we can access that member with in the current package and outside of package in child class only.
- Within the current package we can access protected members either by parent referecne or by child reference, but from outside package we can access protected members only by using child reference.
- In general for instance and static variables it is not required to perform initialization explicitly JVM always provides default values, but for local variables JVM
- won't provide default value, compulsory we have to provide initialization explicitly before using the variables.
- For the normal instance variables it is not required to perform initialization explicitly JVM provides default values, If the instance variables declare as the final then compulsory we have to perform initialization whether we are using or not.
For the normal static variables it is not required to perform initialization explicitly JVM provides default values, for the final static we have to perform initialization whether we are using or not.
- For the local variables JVM won't provide default values, compulsory we have to perform initialization before using it.
- even though local variable declare as the final it is not required to perform initialization if we are not using variable.
- the only applicable modifier for local variable is final.
- Applicable for variable and method but not for class (inner class can be declare as static)
- If the value of variable is varied from object to object then we should go for instance variable, in the case of instance variable for every object a separate copy creates
- If the value of variable is same for all objects then we should go for static variable, in the case of static variable for every object a single copy creates at class level and share among all objects
- static members can be access from both instance and static area where as instance member can be access only from instance member directly
- Applicable only for method
- The main objectives of native keyword is
- To improve performance of system
- To use already existing legacy non java code
- For native methods implementation is already available in other languages and we are not responsible to provide implementation, hence native method declaration should compulsory end with ; syntax - public native void methodName();
- Applicable only for method and block
- If a method or block is declared as synchronized then at a time only one thread is allowed to operate on the given object
- The main advantage of synchronized keyword is we can resolve data inconsistency problems, and main disadvantage of synchronized keyword is it increases waiting time of thread and effects performance of the system. hence if there is no specific requirement there is no need to use synchronized keyword.
- Applicable only for variable
- At the time serialization, if we don't want to save the value of a particular variable to meet security constraints then we should go for transient keyword, at the time of serialization JVM ignores the original value of transient variable and default value will be serialization.
- Applicable only for variable
- If the value of a variable keep on changing such type of variables we have to declare with volatile modifier.
- If a variable declared as volatile then for every thread a separate local copy will be create.
- Every intermediate modification performed by that thread will take place in local copy instead of master copy.
- Once the value got finalize just before terminating the thread the master copy value will be update with local stable value.
- The main advantage of volatile keyword is we can resolve data inconsistency problems, and main disadvantage is volatile keyword is creating and maintaining a separate copy for every thread, increase complexity of programming and effects the performance of system.
- volatile variable means it's value keeps changing.
- The only applicable modifier for local variable is final
- The modifier which are applicable only for variable, but not for class and method are volatile and transient
- The modifier which are applicable only for method but not for class and variable are native and synchronized
- The modifier which are applicable only for top level class, method and variable are public, <default>, final
- The modifier which are applicable only for inner class but not for outer class are private, protected, static