Saturday, April 22, 2023

Static, Final and Access Modifier

 

Static Keyword

The static keyword in Java is used for memory management mainly. We can apply static keyword with variables, methods, blocks and nested classes. The static keyword belongs to the class than an instance of the class.

 

Final Keyword

Java final keyword is a non-access specifier that is used to restrict a class, variable, and method. If we initialize a variable with the final keyword, then we cannot modify its value.

 

Difference between Static and Final

Static                                                                         Final

 

Static keyword can be applied to a                            Final keyword can be applied

Nested class, method and block                                  to method variable and

As well as variable.                                                         classes.

 

Static variable can be change after                             Final variable can't be change

Initialization.                                                                    after initialization.

 

Static method and variable can                                 Object can created to call final

Be accessed directly by class name                            method or final variable.

And does not Object belong to the

Class.

 

Access Modifiers

Access modifiers are keywords that can be used to control the visibility of fields, methods, and constructors in a class.

 

1.     Private: The access level of a private modifier is only within the class. It cannot be accessed from outside the class.

2.     Default: The access level of a default modifier is only within the package. It cannot be accessed from outside the package. If you do not specify any access level, it will be the default.

3.     Protected: The access level of a protected modifier is within the package and outside the package through child class. If you do not make the child class, it cannot be accessed from outside the package.

4.     Public: The access level of a public modifier is everywhere. It can be accessed from within the class, outside the class, within the package and outside the package.

 

Difference between this and super keyword

 

this

super

In Java, this keyword is a reference variable that refers to the current object or instance of a class.

In Java, super keyword is a reference variable that refers to the superclass's object or instance of a class.

It can access variables and methods of the current class.

It can access variables and methods of the parent class or superclass.

Default constructor of the current class can be invoked by this.

Default constructor of the parent class can be invoked by super.

this() can be passed in the method call as an argument.

super() is returned with no arguments.

this() can be passed in the method call as an argument.

 

 

Static, Final and Access Modifier

  Static Keyword The static keyword in Java is used for memory management mainly. We can apply static keyword with variables, methods, blo...