Access Control and Static Keyword in JAVA
Access Control and Static Keyword in JAVA
Lecture 8
Prof. Anita Agrawal,
BITS-Pilani, K.K.Birla Goa campus
Controlling access to members of a class
Use the most restrictive access level that makes sense for a
particular member. Use private unless you have a good reason
not to.
It is applicable to
blocks
variables
nested classes
methods
Static Keyword
Ways of intialisation:
Directly: modifier(if any) static var_type var_name;
Example: public static int x =2;
or……….
Declare a static block
Example: static int x;
static {
x = 2;
}
Any object can change the value of the class variable, but class
variables can also be manipulated without creating an instance
of the class.
Restrictions
They can only access static data
They can call only other static methods
They can’t refer to this or super(Inheritance)