Final, Package, Class Path
Final, Package, Class Path
Final, Package, Class Path
In Java, the final keyword is used to denote constants. It can be used with variables,
methods, and classes.
Once any entity (variable, method or class) is declared final, it can be assigned only
once. That is,
the final variable cannot be reinitialized with another value
the final method cannot be overridden
the final class cannot be extended
In the above program, we have created a final variable named age. And we have
tried to change the value of the final variable.
When we run the program, we will get a compilation error with the following
message.
cannot assign a value to final variable AGE
AGE = 45;
^
2. Java final Method
Before you learn about final methods and final classes, make sure you know about
the Java Inheritance.
In Java, the final method cannot be overridden by the child class. For example,
class FinalDemo {
// create a final method
public final void display() {
System.out.println("This is a final method.");
}
}
In the above example, we have created a final method named display() inside
the FinalDemo class. Here, the Main class inherits the FinalDemo class.
We have tried to override the final method in the Main class. When we run the
program, we will get a compilation error with the following message.
display() in Main cannot override display() in FinalDemo
public final void display() {
^
overridden method is final
class c1(){
public void m1(){
System.out.println("m1 of c1");
}
public static void main(string args[]){
c1 obj = new c1();
obj.m1();
}
}
Here,
1. To put a class into a package, at the first line of code define package p1
2. Create a class c1
3. Defining a method m1 which prints a line.
4. Defining the main method
5. Creating an object of class c1
6. Calling method m1
Step 2) In next step, save this file as demo.java
Step 5) When you execute the code, it creates a package p1. When you open the
java package p1 inside you will see the c1.class file.
class c1{
public void m1() {
System.out.println("m1 of c1");
}
}
This is how the package is executed and gives the output as “m1 of c1” from the
code file.
Let's look at an import statement to import a built-in package and Scanner class.
Example
package myPackage;
import java.util.Scanner;
public class ImportingExample {
int i = read.nextInt();
import java.util.*;
int i = read.nextInt();
These basic question which answers most of the details about PATH and
CLASSPATH in Java are mostly not answered until Java programmer itself acquire
this knowledge, Things may be changed nowadays but important of PATH and
CLASSPATH is still high.
In this article, I'll tell you about the practical difference between PATH and
CLASSPATH environment variables, where are they located, and how exactly they
are used by the Java compiler and JVM. Once you know this basic detail, you would
be able to solve most of the classpath-related problems by yourself.
3. Another significant difference between PATH and CLASSPATH is that PATH can
not be overridden by any Java settings but CLASSPATH can be overridden by
providing command-line option -classpath or -cp to both "java" and "javac"
commands or by using Class-Path attribute in Manifest file inside JAR archive.
4. PATH environment variable is used by the operating system to find any binary or
command typed in the shell, this is true for both Windows and Linux environments
while CLASSPATH is only used by Java ClassLoaders to load class files.
These were some notable differences between PATH vs CLASSPATH in Java and
they are worth remembering to debug and troubleshoot Java-related issues. Though,
I highly recommend you to join these best Java Programming courses to build your
fundamentals in Java.