Getting to know Final in Java - ProgrammerTech
Language:

Search

Getting to know Final in Java

  • Share this:
Getting to know Final in Java

Final concept in Java

The word ( final ) is a reserved word in the Java language and means that this thing or variable that is defined as final is a finite thing that we cannot modify its value. The function defined is of type final. We cannot apply the Override principle to this function, and we cannot modify the value of the defined variable of type final.

 

Things that need to be defined as something of type final

  1. If we want to define a variable and give it a value only once.
  2. If we want to create a class that does not inherit.
  3. To prevent Override if a function is created that cannot be defined again in the class that inherits the function.

Example 1

package a1;
class B
{
    final int=90;
}
public class A1{
     public static void main(String []args){
        
        B a1=new B();
       
        a1.a=80;
     }
}

We have class B and it has a scalar variable a of type final and its value is 90. This indicates that the variable a is a variable whose value cannot be modified as long as it is defined as final. And we downloaded the main program to give it a value of 80, but it will give us an error because it is of the final type. We cannot reset the value.

package a1;
class B
{
    final int=90;
    final void show()
    {
        System.out.println("welcome in programmer tech");
    }
}
public class A1 extends B{
    void show()
    {
    }
     public static void main(String []args){
     }
}

We have class B and it has a scalar variable a of type final and its value is 90. This indicates that the variable a is a variable whose value cannot be modified as long as it is defined as final. Then we created a function of its type final and put a print command for it that this function prints, which is welcome in programmer tech. Then we inherited a son class, which is A1 from class B, and then we tried to present the inheritance by applying the Override principle, which was not accepted, and the reason is that it is of the final type. The Override principle cannot be applied to it because it is one of the final conditions.


Tags:
Muhammad Alush

Muhammad Alush

إسمي محمد تركي العلوش محب للتقنية و التعلم الذاتي, و جميع خبراتي تعلمتها ذاتياً, و اللغات البرمجية التي أتقنها هي html, css, javascript, php, c, python بالإضافة إلى حبي الكبير لمجال الشبكات.

موقعي programmer-tech.com