There are two types of variable in Java classes
1. Class variable (static variable)
2. Instance variable
Class variable (static variable)
Syntax for static variable
public static String staticVariable;
Properties of static variable
Ex. StaticClass.staticVarible;
Properties of instance variable
How Initialize static variables ?
Initialize the static variable at the time of declaration
Example
Output
I am static variable
when the class loaded static variable initialized
Static Initialization Block
syntax for static block
static {
// whatever code is needed for initialization goes here
}
Here is an example of static initialization block
Output
I am static variable
Here we can see that as JVM loads the main class, the static block executed and initialize the variable. No object is created of the class.
Note :
Example of multiple static block
Output
Note :
How to write an immutable Class in Java
1. Class variable (static variable)
2. Instance variable
Class variable (static variable)
The variables defined with "static" keyword is known as class variable, because these are associated with class and created on class memory.
Syntax for static variable
public static String staticVariable;
Properties of static variable
1. These variables are share with all object of the class, and there is a single copy of static variable. if one object change the value of static variable then changes visible to all object.
2. Static variables are initialized only once , at the start of the execution .
3. A static variable can be accessed directly by the class name and doesn’t need any object.
Ex. StaticClass.staticVarible;
4. These variables will be initialized first, before the initialization of any instance variables
Instance variable
The variable define without static keyword is known as "instance" variable or object variable.
Properties of instance variable
1. These variable associated with object of class.
2. Each object have its own local copy for variable.
3. Require instance to access these variable.
How Initialize static variables ?
1. you can initialize it at the time of declaration
2. you can do by making static block
3. alternative to static blocks — you can write a private static method
Initialize the static variable at the time of declaration
Example
public class StaticBlock { private static String staticVariable="I am static variable"; private String instanceVariable; public static void main(String[] args) { System.out.println(staticVariable); } }
Output
I am static variable
when the class loaded static variable initialized
Static Initialization Block
static variable can initialized using static block.
when class loader loads the class static block executed automatically.
syntax for static block
static {
// whatever code is needed for initialization goes here
}
Here is an example of static initialization block
public class StaticBlock { private static String staticVariable; private String instanceVariable; static { staticVariable = "I am static variable"; } public static void main(String[] args) { System.out.println(staticVariable); } }
Output
I am static variable
Here we can see that as JVM loads the main class, the static block executed and initialize the variable. No object is created of the class.
Note :
A class can have any number of static initialization blocks, and they can appear anywhere in the class body. The runtime system guarantees that static initialization blocks are called in the order that they appear in the source code.
Example of multiple static block
public class StaticBlock { private static String staticVariable1; private static String staticVariable2; static { staticVariable1 = "I am static block 1"; System.out.println(staticVariable1); } static { staticVariable2 = "I am static block 2"; System.out.println(staticVariable2); } public static void main(String[] args) { } static { staticVariable2 = "I am static block 3"; System.out.println(staticVariable2); } }
Output
I am static block 1 I am static block 2 I am static block 3
Note :
1. Instance methods can access instance variables and instance methods directly.
2. Instance methods can access static variables and static methods directly.
3. static methods can access static variables and static methods directly.
4. static methods cannot access instance variables or instance methods directly—they must use an object reference. Also, static methods cannot use the this keyword as there is no instance for this to refer to.
How to write an immutable Class in Java
No comments:
Post a Comment