Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Final and Static Keywords in Java

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 166
    Comment on it

    Final and Static Keywords in Java

    The Keyword Final is used for declaring a class member as constant. The member declared as final cannot be overridden.To prevent a class from being inherited the Keyword Final is used. Initialization of a final variable is done inside a class, but not in the constructor of the class. The class declared as a final cannot be Inherited.
    for example:

    final class A 
    {
        // statements
    }
    class B extends A
    {
        //compile time error as Inheritance of final class is not possible 
    
    }
    

    In the above code the class B can't extends class A as it is a final class.

    We cant even override the method of the parent class in inheritance if the function is a final member in the base class.

    Static are variable exists outside of any class. They are usually accessed with the class name using the 'dot' operator.Static members of a class can be Static variables, Static Initialization Block and Static Methods. Static members of a class are stored in the class memory and can be access directly.

    for example:

    class A
    {
        static int a = 5;    //Static Field
    
        static void example()
        {
            //Static method
        }
    }
    
    public class MainClass
    {
        public static void main(String[] args) 
        {
            System.out.println(A.a);    //Accessing static Field 
    
            A.example();     //Accessing static Method 
        }
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: