Content Covered:
- What is the use of final keyword in Java?
- Final Variables
- Final Methods
- Final Classes
- What is the use of static in Java?
- Static Variables
- Static Methods
- Static Blocks
- Static Classes
- What are packages in Java and what types do they have?
- What are default, private, protected and public access protections in Java?
1. Final Keyword in Java:
The final
keyword in Java is used to declare constants, prevent method overriding, and restrict class inheritance. It can be applied to variables, methods, and classes.
1.1 Watch this tutorial alongside for a better understanding of mentioned topics:
1.2 Java Code for final Variable, Method and Class in Java:
public class AllAboutFinalInJava {
public static void main(String[] args) {
int a = 10;
final int b = 12;
a = 12;
b = 14;
classA name = new classA();
}
}
final class classA{
void display1() {
}
}
class classB extends classA{
void display1() {
}
}
2. Static Keyword in Java:
Static Variables: For such variables, the memory is provided only once, during class load, are shared among all the objectives of that class, and should be accessed through class name not through objects of that class.
Static Methods: Such methods, can only access static data (variables and methods), should be accessed through the class name not through objects of that class and and can’t use super and this keyword in their scope.
Static Block: A static block in Java is a block of code inside a class that runs once when the class is loaded into memory. It is declared using static {} and is mainly used for initializing static variables.
Static Classes: Java does not allow a top-level class to be static. However, a nested (inner) class can be static. A static nested class does not depend on an outer class instance.
- Such classes can access only static members of the outer class.
- Such classes can be instantiated without an instance of the outer class.
2.1 Watch this tutorial alongside for a better understanding of mentioned topics:
2.2 Java Code for static variable, method, block and class:
public class AllAboutStaticKeywordInJava {
static int a = 10;
public static void main(String[] args) {
A A1 = new A();
A A2 = new A();
A A3 = new A();
A1.a = 11;
System.out.println(A1.a);//11
System.out.println(A2.a);//10
System.out.println(A3.a);//10
A1.b = 11;
System.out.println(A1.b);//11
System.out.println(A2.b);//11
System.out.println(A3.b);//11
System.out.println(A.b);//11
College c1 = new College("Amit", "123");
System.out.println(c1.name + ", " + c1.roll);
College c2 = new College("Sumit", "124");
System.out.println(c2.name + ", " + c2.roll);
try{
class.forName("College");
}catch (ClassNotFoundException e){
}
NormalClass obj = new NormalClass();
obj.display1();
OuterClass.StaticInnerClass obj2 = new OuterClass.StaticInnerClass();
obj2.display2();
}
}
class A{
int a = 10;
static int b = 10;
}
class College{
String name;
String roll;
static String college;
static{
college = "IITR";
System.out.println("Inside static block");
}
College(String name, String roll){
this.name = name;
this.roll = roll;
}
}
class NormalClass{
void display1() {
System.out.println("Inside display1");
}
}
class OuterClass{
static class StaticInnerClass{
void display2() {
System.out.println("Inside display2: static class");
}
}
}
3. Package and its types in Java:

3.1 Watch this tutorial alongside for a better understanding of mentioned topics:
3.2 Java Code for builtin and user defined packages:
import java.util.ArrayList;
import java.util.List;
public class SampleClass {
public static void main(String[] args) {
System.out.println("Hello");
String s1 = new String("Hello");
List<Integer> list = new ArrayList<>();
}
}
package package1;
public class SampleClass1 {
public static void main(String[] args) {
display1();
}
public static void display1() {
System.out.println("package1.SampleClass1.display1()");
}
}
package package2.part01;
import package1.SampleClass1;
public class SampleClass2 {
public static void main(String[] args) {
SampleClass1.display1();
}
}
4. Access Protections in Java? (Default, Private, Protected and Public)

Hi to all, the contents present at this site are in fact remarkable for people experience, well, keep
up the nice work fellows.