[go: up one dir, main page]

0% found this document useful (0 votes)
21 views18 pages

Final Keyword and Static Keyword

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views18 pages

Final Keyword and Static Keyword

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Final keyword and Static

keyword.
-Mudaliar Krishnakumar.
Topics to be Covered.

1)Static Keyword
a) Static Variable
b) Static Block
c) Static Method
d) Static Class

2)Final Keyword
a) Final Variable
b) Final Method
c) Final Class
Static Keyword

•Shared memory allocation: Static variables and methods are allocated memory space only once
during the execution of the program. This memory space is shared among all instances of the class,
which makes static members useful for maintaining global state or shared functionality.

•Accessible without object instantiation: Static members can be accessed without the need to
create an instance of the class. This makes them useful for providing utility functions and constants
that can be used across the entire program.

•Associated with class, not objects: Static members are associated with the class, not with
individual objects. This means that changes to a static member are reflected in all instances of the
class, and that you can access static members using the class name rather than an object reference.

•Cannot access non-static members: Static methods and variables cannot access non-static
members of a class, as they are not associated with any particular instance of the class.

•Can be overloaded, but not overridden: Static methods can be overloaded, which means that you
can define multiple methods with the same name but different parameters. However, they cannot be
overridden, as they are associated with the class rather than with a particular instance of the class.
1.1 Static Variable

1) Java static variable

If you declare any variable as static, it is known as a static variable.

•The static variable can be used to refer to the common property of all objects
(which is not unique for each object), for example, the company name of
employees, college name of students, etc.

•The static variable gets memory only once in the class area at the time of class
loading.

Advantages of static variable

It makes your program memory efficient (i.e., it saves memory).


1.1 Static Variable
1.2 Static Block

•Is used to initialize the static data member.

•It is executed before the main method at the time of class loading.
1.3. Static Method

1) If you apply static keyword with any method, it is known as static method.

• A static method belongs to the class rather than the object of a class.

• A static method can be invoked without the need for creating an instance of a class.

• A static method can access static data member and can change the value of it.

2)Restrictions for the static method

There are two main restrictions for the static method. They are:

a) The static method can not use non static data member or call non-static method
directly.

b) this and super cannot be used in static context.


1.3. Static Method

When to use static variables and methods?

Use the static variable for the property that is common to all objects. For example, in
class Student, all students share the same college name. Use static methods for
changing static variables.
1.4. Static Class

• We can declare a class static by using the static keyword.


• A class can be declared static only if it is a nested class. It does not require any reference
of the outer class.
• The property of the static class is that it does not allows us to access the non-static
Inner class: of the outer class.
members
The classes that are non-static and nested are called inner classes. Note that we cannot
create an instance of the inner class without creating an instance of the outer class. Without
using the reference to the outer class instance, an instance of the inner class can access the
members of its outer class. It makes the program simple and concise.

Outer Class:
The class in which nested class is defined is called outer class.

Nested Class:
Java allows us to define a class within a class that is known as a nested class. It may be
static or non-static. The major difference between static and non-static class is that:
•An instance of the static nested class can be created without creating an instance of its outer
class.
•The static and non-static members of an outer class can be accessed by an inner class.
1.4. Static Class

Remember:
•All static classes are nested classes but vice-versa is not true.

•It can access only static members of the outer class.

•Non-static variable and instance methods cannot be accessed within the static class. If you
try to access a non-static reference from a static field, it throws an error: Cannot make a
static reference to the non-static field.

•We can create static blocks, variables, and methods inside a static class.

•A class may have multiple static classes.

•We cannot access the static class if it is inside the static block.

•There may be any number of static classes within a static class.


2 Final Keyword

WHY FINAL KEYWORD IS USED?

•Initialization: Final variables must be initialized either at the time of declaration or in the
constructor of the class. This ensures that the value of the variable is set and cannot be
changed.

•Performance: The use of final can sometimes improve performance, as the compiler can
optimize the code more effectively when it knows that a variable or method cannot be
changed.

The final keyword


•Security: final can in java
help is used
improve to restrict
security the user. The
by preventing java final
malicious codekeyword can be used in
from modifying
many context.
sensitive Final
data or can be:
behavior.
1.variable
2.method
3.class

The final keyword can be applied with the variables, a final variable that have no value it is
called blank final variable or uninitialized final variable. It can be initialized in the constructor
only. The blank final variable can be static also which will be initialized in the static block only.
2.1 Final Variable.

Final variables:
• When a variable is declared as final, its value cannot be changed once it has been
initialized.

• This is useful for declaring constants or other values that should not be modified.
2.2 Final Method.

Final methods:
• When a method is declared as final, it cannot be overridden by a subclass.

• This is useful for methods that are part of a class’s public API and should not be modified
by subclasses.
2.2 Final Method

Is final method inherited?

Ans) Yes, final method is inherited but you cannot override it. For Example:

Output : running…
2.3 Final Class

Final classes:
• When a class is declared as final, it cannot be extended by a subclass.

• This is useful for classes that are intended to be used as is and should not be modified or
extended.

There are two uses of a final class:

Usage 1: One is definitely to prevent inheritance, as final classes cannot be extended. For
example, all Wrapper Classes like Integer, Float, etc. are final classes. We can not extend
them.

Usage 2: The other use of final with classes is to create an immutable class like the
predefined String class. One can not make a class immutable without making it final.
2.3 Final Class

Output: Compile Time Error….


Advantages of Final Keyword
The final keyword in Java provides several advantages, including:
•Ensuring immutability: When a variable or reference is marked as final, its value cannot
be changed once it is assigned. This helps ensure that data is immutable and cannot be
accidentally or maliciously modified.

•Improving performance: The use of final can sometimes help improve performance, as
the Java Virtual Machine (JVM) can optimize code more effectively when it knows that certain
values or references cannot be changed.

•Making code easier to understand: By declaring variables, methods, or classes as final,


developers can make their code easier to understand and reason about. When a value or
reference is marked as final, it is clear that it will not change, which can simplify code
analysis and debugging.

•Promoting code reuse: By declaring methods as final, developers can prevent subclasses
from overriding them. This can help promote code reuse and reduce duplication, as
subclasses must use the parent class’s implementation of the method.

•Enhancing security: The use of final can help enhance security by preventing malicious
code from modifying sensitive data or behavior.
THANK YOU

You might also like