static member in java code example
Example 1: static in java
static keyword is a non-access modifier. static keyword can be used with
class level variable, block, method and inner class or nested class.
Example 2: static in java
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.
The static can be:
Variable (also known as a class variable)
Method (also known as a class method)
Block
Nested class
Example 3: static member in java
static keyword: belongs to the class, also can be called through the class
static variable: declared outside any block with static keyword
static: runs first, only runs one time
static member:
1. static variables
2. static methods
3. static initializer block
4. static inner class(nested class)
static methods: methods that we can call it through the class name.
belongs to the class
Ex: Webdriver driver = WebdriverFactory.getDriver();
********* Static methods only accepts class member(static) ************
********* None static can ONLY be called through the object ***********
Static only accept static. Anything not static you cannot call directly.
You can call Instance variable in Constructor.
You cannot call instance variable in static you have to create object first.
You can call non static (instance variable) in the instance block.
You can call static in the instance block
Static variable da last initialization will be final value.