Core Java Static
Core Java Static
Static is a keyword, applicable to the classes, variables, methods, and blocks. The class members,
class, and blocks can be made static using “static” keyword in front of the name of the class
members, class, and blocks respectively. When a class member is declared as static, it becomes
global for all other members of the class. The static member of the class does not occupy
memory on per instance basis, i.e. all the objects shares the same copy of static member. The
static member can be used independently of any object of that class. You can access the static
member of the class before its object is created. The best example of the static member is main( )
method, it is declared static so that it can be invoked before any object exists. The general form
to access the static member of the class:
In above code class_name is the name of the class in which the static_member is defined. Static
member can be a static variable or static method
Static variables:
A static variable acts like a global variable for all other data members of the class.
A static variable can be accessed before any object of the class exists.
A static variable can be accessed with the class name in which it is defined followed by the
dot(.) operator.
Static Methods:
Static class:
Java does have the concept of nested static class. The outermost class can not be made
static whereas the innermost class can be made static.
A static nested class can not access the non-static member of the outer class.
It can only access the static members of the outer class.
Static Block:
Static block is executed only once when the class is loaded. Used to initialize the static variables
of the class.
C++:
In C++ we have the concept of static variables as well as static functions whereas, C++ do not
support static class.
C#:
Java:
Definition of Final
Final is a keyword applicable to the class, variable and methods. The class, variable and the
method is declared as final using the keyword “final” preceded by their name. Once a variable is
declared as final; it can not be modified further in the program. A final variable must be
initialized at the time declaration. Final variables do not occupy memory on per-instance basis.
All the objects of the classes share the same copy of the final variable.
The method declared as final can not be overridden by the subclass of that class in which final
method is declared. When a class is declared as final other class can not inherit that final class.
C++, C# do not support the concept if final keyword. Java supports the concept of final keyword
and in Java; class, variable, and method can be declared as final.