Restrictions on static methods
A static method belongs to the class rather than the object.
- The static method can not use non-static data member or call the non-static method directly.
- this and super cannot be used in static context as they are non-static.
Because a static method is invoked without a class instance we cannot use this or super reference in static method.It is illegal to reference any of the class fields or methods using a reference within a static method.
1.The static method cannot use non static data member or call non static method directly.
2.This and super keywords cannot be used in static context.
3.Access only static type data(static type instance variable).
4.Call only static method if non static then compile time error.
5.No need of the class object to call the static method.
6.Cannot use this and super keywords otherwise compile time error
The static keyword in Java is used for memory management
The static can be:
- Nested class
A static method belongs to the class rather than the object of a class.A static method can be invoked without the need for creating an instance of a class. A static method can access static data member and can change the value of it.
There are two main restrictions for the static method. They are:
- The static method can not use non static data member or call non-static method directly.
- this and super cannot be used in static