10.1Access Control
Friday, August 21, 2009
As you know, encapsulation links data with the code that manipulates it. However, encapsulation provides another important attribute access control.
Java’s access specifier are public, private and protected.Java also defines a default access level.protected applies only when inheritance is involved.
Let’s begin by defining public and private.When a member of a class is modified the public specifier, then that member can be accessed by any other code. When a member of class is specified as private, then that member can only be accessed by other members of its class.
So, we can understand that why main() has always been preceded by the public specifier.It is called by code that is outside the program-that is, by the Java run-time system.
When no access specifier is used, then by default the member of a class is public within its own package, but cannot be accessed outside of its package.
To understand the effects of public and private access, have a look at the fallowing example
As you can see, inside the Access class, x uses default access, y is explicitly specified as public.Field z is given private access.
Inside the main class, z cannot be used directly. It must be accessed through its public methods :setZ() and getZ().
Static Fields and Methods
These are used to define class variables and methods that belong to a class and not to any particular instance of the class. static associates the data members with a class and not the objects of the class.
Whn a member is declared static, it can be accessed before any objects of its class are created, and without reference to any object.The most common example of astatic member is main().
main() is declared as static because it must be called before any object exists.
Fields declared as static are, essentially, global variables.When objects of its class are declared, no copy of static variable is made.
All instances of the class share the same static variable.
If we want to declare a method as static we need to fallow the several restrictions
1. static methods can only call other static methods.
2. They must only access static data.
3. We cannot refer to this or super in any way.
If we want to manipulate in order to initialize our static variables, we can declare a static block which gets executed exactly once, when the class is first loaded.
Have a look at the fallowing examples to know more about static
From the above example we can observe, when the Main class is loaded, all of the static statements executed first. Initially x is set to 4, then the static block executed and finally y is initialized to 12.
Here is the output of the program
Static Block.......
Value of A : 3
Value of X : 4
Value of Y : 12
If you wish to call a static method from outside its class, you can do so using the fallowing general form.
classname.method()
Here is an example to demonstrate the above situation.
Here you can see that inside main(), the static method calc() and the static variable j are accessed outside of their class.
Here is the output of the program
Value of I : 10
Value of J : 20
final Keyword
In Java a variable can be declared as final.By doing this we can prevent its contents from being modified. Which means that we must initialize final variable when it is declared.final is similar to const in C/C++/C#.
For example have look at the fallowing declarations
Java’s access specifier are public, private and protected.Java also defines a default access level.protected applies only when inheritance is involved.
Let’s begin by defining public and private.When a member of a class is modified the public specifier, then that member can be accessed by any other code. When a member of class is specified as private, then that member can only be accessed by other members of its class.
So, we can understand that why main() has always been preceded by the public specifier.It is called by code that is outside the program-that is, by the Java run-time system.
When no access specifier is used, then by default the member of a class is public within its own package, but cannot be accessed outside of its package.
To understand the effects of public and private access, have a look at the fallowing example
// Access.java demonstrates the difference between public and private
/**
*
* @author Sudarsan
*/
class Access {
// Defining three fields with different access levels
int x;
public int y;
private int z;
//To initilize field Z
void setZ(int a)
{
z=a;
}
//To get Z value
int getZ()
{
return z;
}
}
// Creating Access Object
/**
*
* @author Sudarsan
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Access ob=new Access();
ob.x=10;
ob.y=20;
//This will cause an error because z is private in Access
// ob.z=30;
ob.setZ(30); // To initialize z we need to use this method
System.out.println("The Values of X, Y and Z are :"+ob.x+" "+ob.y+" "+ob.getZ());
}
}
As you can see, inside the Access class, x uses default access, y is explicitly specified as public.Field z is given private access.
Inside the main class, z cannot be used directly. It must be accessed through its public methods :setZ() and getZ().
Static Fields and Methods
These are used to define class variables and methods that belong to a class and not to any particular instance of the class. static associates the data members with a class and not the objects of the class.
Whn a member is declared static, it can be accessed before any objects of its class are created, and without reference to any object.The most common example of astatic member is main().
main() is declared as static because it must be called before any object exists.
Fields declared as static are, essentially, global variables.When objects of its class are declared, no copy of static variable is made.
All instances of the class share the same static variable.
If we want to declare a method as static we need to fallow the several restrictions
1. static methods can only call other static methods.
2. They must only access static data.
3. We cannot refer to this or super in any way.
If we want to manipulate in order to initialize our static variables, we can declare a static block which gets executed exactly once, when the class is first loaded.
Have a look at the fallowing examples to know more about static
// Using static variables, methods and blocks
/**
*
* @author Sudarsan
*/
public class Main {
/**
* @param args the command line arguments
*/
static int x=4;
static int y;
static void calc(int a)
{
System.out.println("Value of A : "+a);
System.out.println("Value of X : "+x);
System.out.println("Value of Y : "+y);
}
static
{
System.out.println("Static Block.......");
y=x*3;
}
public static void main(String[] args) {
// TODO code application logic here
calc(3);
}
}
From the above example we can observe, when the Main class is loaded, all of the static statements executed first. Initially x is set to 4, then the static block executed and finally y is initialized to 12.
Here is the output of the program
Static Block.......
Value of A : 3
Value of X : 4
Value of Y : 12
If you wish to call a static method from outside its class, you can do so using the fallowing general form.
classname.method()
Here is an example to demonstrate the above situation.
// Static.java
/**
*
* @author Sudarsan
*/
class Static {
static int i=10;
static int j=20;
static void calc()
{
System.out.println("Value of I : "+i);
}
}
// Main class is used to call static method
/**
*
* @author Sudarsan
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Static.calc();
System.out.println("Value of J : "+Static.j);
}
}
Here you can see that inside main(), the static method calc() and the static variable j are accessed outside of their class.
Here is the output of the program
Value of I : 10
Value of J : 20
final Keyword
In Java a variable can be declared as final.By doing this we can prevent its contents from being modified. Which means that we must initialize final variable when it is declared.final is similar to const in C/C++/C#.
For example have look at the fallowing declarations
final int VAR1=1;
final int VAR2=10;
Labels:
Methods