Java Tutorials 31 - Use of static Keyword in Java - StudyViral
Use of static keyword in Java
static keyword is used in java with following things
1). variables (class variables)
2). methods (class methods)
3). block (i.e. static block)
4). nested class
5). interface static methods (Java 8 onwards..)
6). static with import statement
The static keyword is used in java mainly for memory management. It is used with variables, methods, blocks and nested class. It is a keyword that are used for share the same variable or method of a given class.
Java static Variable
Static import allows you to access the static member of a class directly without using the fully qualified name.
e.g.:: import static java.lang.System.out;
This topic will covered with package in Java.
Use of static keyword in Java
static keyword is used in java with following things
1). variables (class variables)
2). methods (class methods)
3). block (i.e. static block)
4). nested class
5). interface static methods (Java 8 onwards..)
6). static with import statement
The static keyword is used in java mainly for memory management. It is used with variables, methods, blocks and nested class. It is a keyword that are used for share the same variable or method of a given class.
Java static Variable
- We can use static keyword with a variable.
- A static variable is a class variable and doesn’t belong to Object/instance of the class.
- When a variable is declared as static, then a single copy of variable is created and shared among all objects at class level.
- When a method is declared with static keyword, it is known as static method.
- A static method can only directly call other static methods.
- A static method can only directly access static data/variables.
- A static method cannot refer to this or super keyword.
- The most common example of a static method is main() method.
- Java static block is the group of statements that gets executed when the class is loaded into memory by Java CIassLoader.
- Static block is used to initialize static variables of the class.
- Mostly it’s used to create static resources when class is loaded.
- We can’t access non-static variables in static block.
- We can have multiple static blocks in a class.
- A static block that gets executed exactly once, when the class is first loaded.
Static import allows you to access the static member of a class directly without using the fully qualified name.
e.g.:: import static java.lang.System.out;
This topic will covered with package in Java.
0 comments:
Post a Comment