When a member is declared static, it can be accessed before any objects of its class are created, and without reference to any object. You can declare both methods and variables to be static. The most common example of a static member is main( ).
Variables
Instance variables declared as static are, essentially, global variables. When objects of its class are declared, no copy of a static variable is made. Instead, all instances of the class share the same static variable.
Methods
Methods declared as static have several restrictions:
• They can only call other static methods.
• They must only access static data. They can not to refer to any instance variables.
• They cannot refer to this or super in any way.
If you need to do computation in order to initialize your static variables, you can declare a static block which gets executed exactly once, when the class is first loaded.
No comments:
Post a Comment