Hello,
After many ages, I have started writing this post. My 11th
post. Its not that I am busy, its just that I’ve got other priorites.. We
don’t know what we miss, until we miss it :D
Coming back to Java, this post is completely dedicated to Access
Modifiers. Yes, as the name suggests access modifiers help us to modify the
access of any class, methods or variables. In other words, using these
modifiers we get the control of their visibility. Remember a blind rule, if we
have visibility of any class or method or variable, then we can access them if we want. Something like, you get what you see..!
In Java we have 3 types of access modifiers.
- public – visible to entire world
- protected – visible ONLY to children
- private – NOT visible to anyone
Before we dig into the details, I wanna clarify few things.
- Java class can have ONLY public or no access modifier at all (default package level access)
- For class members like methods and variables all 4 modifiers are applicable.
- For class variables (instance variables), if we do not define any value, JVM will assign default values depending on the type you define.
- For local variables, no modifiers are accepted except final.
- Local variables need to be declared. The compiler will complain if you do not declare any value after defining them.
Let’s see how a java class with instance variables, methods
and local variables looks like.
public class TypicalClass {
//instance variables - JVM will assign default value if we have not define
private String instanceVariableString; // private
public int instanceVariableInteger; // public
protected char instanceVariabeChar = 'S'; // protected
long instanceVariableLong; // default
// methods
String method1(){
//local variables - local variables CANNOT take any other modifier
// except "final"
double localDoubleVariable = 123D;
final String localFinalString = "";
System.out.println("TypicalClass.method1()");
return instanceVariableString;
}
protected int method2(){
System.out.println("TypicalClass.method2()");
return instanceVariableInteger;
}
private char method3(){
System.out.println("TypicalClass.methid3()");
return instanceVariabeChar;
}
public long method4(){
System.out.println("TypicalClass.method4()");
return instanceVariableLong;
}
}
public
A class/method/variable can use public keyword. When you
declare class/method/variable as public, regardless of package all classes can
use the public class and its members. Below example will give you more idea how
we can access members of another Java class.
package firstPackage;
// import the class from different package
import secondPackage.*;
class FirstPackageClass {
public static void main(String[] args){
// create an object of SecondPackageClass
SecondPackageClass spc = new SecondPackageClass();
//using the object, access the members of SecondPackageClass class using dot(.) operator
spc.testId();
}
}
// different class in different package
package secondPackage;
public class SecondPackageClass {
public void testIt(){
System.out.println("secondPackageClass.testIt()");
}
}
As you can see, we access the members of other class
using the dot operator. Below are the point you need to keep in mind, if you
want to access the members of any other class from different package.
- Make sure if the class you want to access is public.
- Import the class.
- Create an object for the class you want access.
- With the object, access the members using dot operator.
private
A class cannot be marked as private. But the members in the class can be marked as private. The
methods or variables marked as private cannot
be accessed by any other class in the world. These private members can ONLY be
used in which the private member is declared.
So be aware of the private
access modifier, if you are sure that no one wants to access your code then mark it as private.
See the below example,
package firstPackage;
class FirstClass {
public static void main(String[] args){
// create an object of SecondClass. This is possible because the
// the SecondClass is public
SecondClass spc = new SecondClass();
// this is fine, because the testPublicVariable is public
System.out.println("Access the SecondClassinstance variable --> "+spc.testPublicVariable);
//using the same object, we try to access testIt method. This method is private.
// So if we try to access this methis, the compiler will complain saying --
// how can we access something which we doesnt even know that exists.
spc.testId(); // compiler error at this line
}
}
// another class in the same package
public class SecondClass {
public String testPublicVariable = "LindlarsForLife";
private void testIt(){
System.out.println("SecondClass.testIt()");
}
}
If we try to compile FirstClass, we get an
error something like this,
Cannot find symbol Symbol : spc.testId()
We will see about protected and default in the next post. Protected is what the most interesting access modifier using the big feature Inheritence. Stay tuned..:P
:)
0 comments:
Post a Comment