The static keyword in Java is used for memory management
The static keyword belongs to the class than an instance of the class.
Static can be : Variable, Method, Block , Nested class
static Variable:
1. Static variable is used to fulfil the common requirement. For Example company name of employees, college name of students etc. Name of the college is common for all students
2. static variable gets memory only once in the class area at the time of class loading.
static Method:
1. A static method belongs to the class rather than the object of a class.
2. A static method can be invoked without the need for creating an instance of a class.
3. A static method can access static data member and can change the value of it.
Restrictions -
--The static method can not use non static data member or call non-static method directly.
--this and super cannot be used in static context.
Why main method is static?
It is because the object is not required to call a static method. If it were a non-static method, JVM creates an object first then call main() method that will lead the problem of extra memory allocation.
Difference between static and final keyword:
static keyword always fixed the memory that means that will be located only once in the program where as final keyword always fixed the value that means it makes variable values constant
Java static Block:
Is used to initialize the static data member.
It is executed before the main method at the time of classloading.
To get more details please watch below youtube video and Subscribe the channel.
Part1:
The static keyword belongs to the class than an instance of the class.
Static can be : Variable, Method, Block , Nested class
static Variable:
1. Static variable is used to fulfil the common requirement. For Example company name of employees, college name of students etc. Name of the college is common for all students
2. static variable gets memory only once in the class area at the time of class loading.
static Method:
1. A static method belongs to the class rather than the object of a class.
2. A static method can be invoked without the need for creating an instance of a class.
3. A static method can access static data member and can change the value of it.
Restrictions -
--The static method can not use non static data member or call non-static method directly.
--this and super cannot be used in static context.
Why main method is static?
It is because the object is not required to call a static method. If it were a non-static method, JVM creates an object first then call main() method that will lead the problem of extra memory allocation.
Difference between static and final keyword:
static keyword always fixed the memory that means that will be located only once in the program where as final keyword always fixed the value that means it makes variable values constant
Java static Block:
Is used to initialize the static data member.
It is executed before the main method at the time of classloading.
To get more details please watch below youtube video and Subscribe the channel.
Part1:
Part2:
hi sir
ReplyDeleteAll sessions are nice
Thank You..!!!
Delete