0% found this document useful (0 votes)
15 views19 pages

Java Static and Final 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)
15 views19 pages

Java Static and Final 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/ 19

Java Static and Final Keyword

Static Keyword

Variables

Static Methods

Block

Class

• ‘Static’ keyword can be used in four ways


Static Variable

• Java static variable is common to all objects or instances of a class


• Or a single copy of the variable is shared to all the objects
• Memory allocations happens when the class is loaded to the memory

Syntax

static data-type variable-name;


public class staticVariableDemo {

static int x = 12;

public static void main(String[] args) {

staticVariableDemo obj1 = new staticVariableDemo();


staticVariableDemo obj2 = new staticVariableDemo();

System.out.println(x);

x = 10;
System.out.println(x);
• Variables declared as ‘static’ can be accessed without the use of obje
obj1.x = 18;
obj2.x = 22;
System.out.println(obj1.x);
System.out.println(obj2.x); The value of ‘static’ variable is shared among multiple objects

x = 0;
System.out.println(obj1.x);
System.out.println(obj2.x);
output
} 12
} 10
22
22
0
0
Java Static Methods

Syntax

static return-type function-name()

• Static methods can access static variables without using objects


• Note that non-static variables and non-static methods can only be accessed using objects
• Static method can be accessed from static and non-static method
• Static method can access static variables without the use
of objects

public class staticVariableDemo {

static int x = 12;;

public static void main(String[] args) {

staticVariableDemo obj1 = new staticVariableDemo();


staticVariableDemo obj2 = new staticVariableDemo();

System.out.println(x);

x = 10;
System.out.println(x);

}
}
public class staticMethodDemo1 { • Static method can be called from static and non-static methods

static void show() {

System.out.println("static method called");


}

void nonStaticMethod() {

Non-static method
System.out.println("non-static method called");

show();
}

public static void main(String[] args) {

show();
Static method
staticMethodDemo1 obj1 = new
staticMethodDemo1();
obj1.nonStaticMethod();

} output
} static method called
non-static method called
static method called
Java Static Block

• Static block is used to initialize the static variables


• Static block is executed when the class is loaded to memory
• A program can have multiple static blocks and it will get executed in the order in which it is writte

Syntax

static {

}
public class staticBlock1 {

static int x;
static String s; Static variables
static {
System.out.println("static block executed");
x = 1; Static block
s = "static block";
}

public static void main(String[] args) {

System.out.println("Value of x : "+x);
System.out.println("Value of String : "+s);

}
• Static block is executed when the class is loaded to the memory
}

output
static block executed
Value of x : 1
Value of String : static block
public class staticMultipleBlocks1 { • Multiple static blocks
• Observe the order of execution of static blocks
static int x; • Static blocks are executed when the class is
static String s; loaded to the memory
• Observe the value displayed in the ‘main’ method
static {
System.out.println("Block1");
x = 1; Block 1
s = "Block1";
}

static {
System.out.println("Block2");
x = 2; Block 2
s = "Block2";
}

public static void main(String[] args) {


System.out.println("value of x: "+x);
System.out.println("value of String :"+ s);
}

} output
Block1
Block2
value of x: 2
value of String :Block2
public class staticMultipleBlocks1 {

static int x;
static String s;

static {
System.out.println("Block1");
x = 1;
s = "Block1";
}

public staticMultipleBlocks1() {
System.out.println("Constructor called"); What is happening here ?
x = 2;
s = "constructor";
}

static {
System.out.println("Block2");
x = 3;
s = "Block2";
}

public static void main(String[] args) {


staticMultipleBlocks1 obj1 = new staticMultipleBlocks1();

System.out.println("value of x: "+x);
output
Block1
System.out.println("value of String :"+ s); Block2
Constructor called
} value of x: 2
} value of String :constructor
Programming Assignment 6 Question for Next week:

1. What are java inner classes ?


2. What are the different ways of defining inner classes in java ?
‘Final’ Keyword

‘Final’ keyword is used in java when variable, method, and class definitions shouldn’t change

Variables

Methods
Final

Class
Final Variable

• Variables when declared as final, their values become constants


• The value cant be changed after it is initialized

public class finalVariable1 {


public class finalVariable1 {
final int x = 10;
final int x = 10;
public static void main(String[] args) {
public static void main(String[] args) {
finalVariable1 obj1 = new finalVariable1();
finalVariable1 obj1 = new finalVariable1();
obj1.x = 20;
System.out.println("value of x: "+obj1.x);
System.out.println("value of x: "+obj1.x);
}
}
}
}

Make observation from the output Make observation from the output
Blank Final Variable

• Final variable which is not initialized at the time of declaration is known as blank final variable
• Blank final variable must be initialized in the constructor, else it will throw error

public class blankFinalVariable1 {


public class blankFinalVariable1 {
final int x;
final int x;
public blankFinalVariable1() {
public static void main(String[] args) {
x = 100;
}
blankFinalVariable1 obj1 = new blankFinalVariable1();
public static void main(String[] args) {
obj1.x = 100;
blankFinalVariable1 obj1 = new blankFinalVariable1();
}
System.out.println("value is: "+obj1.x);
}
}

Make observation from the output Make observation from the output
public class blankFinalVariable1 {

final int x;

static {
x = 100;
}

public static void main(String[] args) {

blankFinalVariable1 obj1 = new blankFinalVariable1();

System.out.println("value is: "+obj1.x);

}
Can the final variable be initialized in a static block ?
If not then how this can happen
public class blankFinalVariable1 {

static final int x;

static {
x = 100;
}

public static void main(String[] args) {

blankFinalVariable1 obj1 = new blankFinalVariable1();

System.out.println("value is: "+obj1.x);

} • Static final variable which is not initialized at the


}
time of declaration can only be initialized in a static block

Make observation from the output


Final method

• Methods with keyword final cannot be overridden

Final class

• Class with keyword final cannot be extended


Programming Assignment 6 Question for Next week:

3. Explain ‘final method’ with proper examples


4. Explain ‘final class’ with proper examples

You might also like