Access Specifiers:
In order to Restrict the access over the programs we have 3 types of access specifiers
1.Public
2.Private
3.Protected
Access Specifier Table
specifier/modifier | local variable | instance variable | method | class |
---|
public | NA | A | A | A |
protected | NA | A | A | NA |
default | A | A | A | A |
private | NA | A | A | NA |
final | A | A | A | A |
static | NA | A | A | NA |
synchronized | NA | NA | A | NA |
native | NA | NA | A | NA |
volatile | NA | A | NA | NA |
transient | NA | A | NA | NA |
strictfp | NA | NA | A | A |
A: Allowed NA: Not Allowed
Public: |
---|
public classes, methods, and fields can be accessed from everywhere. The only constraint is that a file with Java source code can only contain one public class whose name must also match with the filename. If it exists, this public class represents the application or the applet, in which case the public keyword is necessary to enable your Web browser or appletviewer to show the applet. You use public classes, methods, or fields only if you explicitly want to offer access to these entities and if this access cannot do any harm. An example of a square determined by the position of its upper-left corner and its size: |
public class Square { // public class |
public x, y, size; // public instance variables |
} |
Protected: |
---|
protected methods and fields can only be accessed within the same class to which the methods and fields belong, within its subclasses, and within classes of the same package, but not from anywhere else. You use the protected access level when it is appropriate for a class's subclasses to have access to the method or field, but not for unrelated classes. |
default (no specifier) |
---|
If you do not set access to specific level, then such a class, method, or field will be accessible from inside the same package to which the class, method, or field belongs, but not from outside this package. This access-level is convenient if you are creating packages. For example, a geometry package that contains Square and Tiling classes, may be easier and cleaner to implement if the coordinates of the upper-left corner of a Square are directly available to the Tiling class but not outside the geometry package. |
Private: |
---|
private methods and fields can only be accessed within the same class to which the methods and fields belong. private methods and fields are not visible within subclasses and are not inherited by subclasses. So, the private access specifier is opposite to the public access specifier. It is mostly used for encapsulation: data are hidden within the class and accessor methods are provided. An example, in which the position of the upper-left corner of a square can be set or obtained by accessor methods, but individual coordinates are not accessible to the user. |
public class Square { // public class |
private double x, y // private (encapsulated) instance variables |
public setCorner(int x, int y) { // setting values of private fields |
this.x = x; |
this.y = y; |
} |
public getCorner() { // setting values of private fields |
return Point(x, y); |
} |
} |
Static and Non Static Member Functions:
Static Functions: |
---|
It is a method which belongs to the class and not to the object(instance) |
A static method can access only static data. It can not access non-static data (instance variables) |
A static method can call only other static methods and can not call a non-static method from it. |
A static method can be accessed directly by the class name and doesn’t need any object |
Syntax : <class-name>.<method-name> |
A static method cannot refer to “this” or “super” keywords in anyway |
Non-Static Function: |
---|
It is a method which belongs to the class and it cannot be accessed without the object of the class. |
Static and Non Static member variable:
Static Variable: |
---|
It is a variable which belongs to the class and not to object(instance) |
Static variables are initialized only once,at the start of the execution.These variables will be initialized first, before the initialization of any instance variables |
A single copy to be shared by all instances of the class |
A static variable can be accessed directly by the class name and doesn’t need any object |
Syntax : <class-name>.<variable-name> |
Non -Static Variable: |
---|
It doesnt retain/update the value . |
It doesnt have the global scope because its usually act as local variable. |
-
- import java.io.*;
-
- class student {
-
- static int count;//Does not Require initialization.
- int count1 = 0;//Non Static Member Variable must to be initialized
-
- student() {
- acc_details();//Accessed only within the class
- }
-
- public static void stu_details()//static method can be called without the object
- {
- System.out.println("Name:XXXX");
- System.out.println("Roll No:123456");
- count++;
- //count1++;here it cannot be used because non static member could not be used in the static function
- }
-
- private void acc_details()//non static method can be called only with the object
- {
- System.out.println("Entered into Private Access Specifiers");
- System.out.println("Password:*******");
- count++;
- count1++;
- }
-
- protected void att_details()//non static method can be called only with the object
- {
- System.out.println("Attendance Details");
- count++;
- count1++;
-
- }
- }
-
- class staff extends student {
-
- protected static void sta_details()//static method can be called without the object
- {
- count++;
- //count1++;here it cannot be used because non static member could not be used in the static function
- System.out.println("Name:YYYY");
-
- }
- }
-
- class hod extends staff {
-
- protected static void hod_details()//static method can be called without the object
- {
- count++;
-
- //count1++;here it cannot be used because non static member could not be used in the static function
-
- System.out.println("Name:ZZZ");
-
- }
- }
-
- class college extends hod {
-
- public static void main(String a[]) {
-
-
- stu_details();//College can view the student details because it is in public mode
- /*
- * static method can be called without the object .If you didnt specify
- * as static during the declaration of this function you will get an
- * error message during the calling of this function
- */
-
- sta_details();//College can view the staff details because it is in public mode
- /*
- * static method can be called without the object .If you didnt specify
- * as static during the declaration of this function you will get an
- * error message during the calling of this function
- */
-
- hod_details();//College can view the hod details because it is in public mode
- /*
- * static method can be called without the object .If you didnt specify
- * as static during the declaration of this function you will get an
- * error message during the calling of this function
- */
-
-
- staff s1 = new staff();
- s1.stu_details();//staff can also view the student details because it is in public mode
-
-
- hod s2 = new hod();
- s2.stu_details();//hod can also view the student details because it is in public mode
-
- s1.att_details();//staff can also view the student attendance details because it is inherited so it has an access over protected details.
-
-
- s2.att_details();//staff can also view the student attendance details because it is inherited so it has an access over protected details.
-
-
- //acc_details() cannot not be viewed by any of the classes like staff,hod and college becuase it is in private mode.
- student s = new student();
- //s.acc_details(); it cannot be called because private mode function only accessed within the function.
- s.stu_details();
- s.att_details();
-
- System.out.println("Count value without object:" + count);//count variable can be called without an object
- //System.out.println("Count1 Value:" + count1); count1 variable cannot be called without an object because it is non-static
- System.out.println("Count value with object of class student:" + s.count);
- System.out.println("Count value with object of class staff:" + s1.count);
- System.out.println("Count value with object of class hod:" + s2.count);
- System.out.println("Count1 value with object of class student:" + s.count1);
- System.out.println("Count1 value with object of class staff:" + s1.count1);
- System.out.println("Count1 value with object of class hod:" + s2.count1);
- }
- }