Wednesday, 29 April 2015

Java Source World: What are access specifiers and access modifiers

What are access specifiers and access modifiers | Java Source World


                                                                                       Java Source World                   A Java quick reference blog


 What are access specifiers and access modifiers in java



          Access specifiers                                      Access modifiers

             Public                                                                         Public
             Protected                                                                  Abstract
             Private                                                                       Final
                                                                                                 Static
                                                                                                 Volatile
                                                                                                 Synchronized
                                                                                                 Transient
                                                                                                  Native

  1.  Public : The Variables and methods can be access any where and any package.
  2.  Protected : The Variables and methods can be access same Class, same Package & sub class.
  3.  Private : The variable and methods can be access in same class only.
Same class - Public, Protected, and Private
Same-package & subclass - Public, Protected
Same Package & non-sub classes - Public, Protected
Different package & Sub classes - Public, Protected
Different package & non- sub classes - Public

Identifiers : are the Variables that are declared under particular Data type.
Literals: are the values assigned to the Identifiers.
Static : access modifier. Signature: Variable-Static int b; Method- static void meth(int x)

When a member is declared as Static, it can be accessed before any objects of its class are
created and without reference to any object. Eg : main(),it must call before any object exit.
  1. Static can be applied to Inner classes, Variables and Methods.
  2. Local variables can’t be declared as static.
  3. A static method can access only static Variables. and they can’t refer to this or super in anyway.
  4. Static methods can’t be abstract.
  5. A static method may be called without creating any instance of the class.
Only one instance of static variable will exit any amount of class instances.
Final : access modifier
  1. All the Variables, methods and classes can be declared as Final.
  2. Classes declared as final class can’t be sub classed.
  3. Method ‘s declared as final can’t be over ridden.
  4. If a Variable is declared as final, the value contained in the Variable can’t be changed.
  5. Static final variable must be assigned in to a value in static initialized block.
Transient : access modifier
  1. Transient can be applied only to class level variables.
  2. Local variables can’t be declared as transient.
  3. During serialization, Object’s transient variables are not serialized.
  4. Transient variables may not be final or static. But the complies allows the declaration and no compile time error is generated.

Volatile : access modifier
  1. Volatile applies to only variables.
  2. Volatile can applied to static variables.
  3. Volatile can not be applied to final variables.
  4. Transient and volatile can not come together.
  5. Volatile is used in multi-processor environments.
Native : access modifier
  1. Native applies to only to methods.
  2. Native can be applied to static methods also.
  3. Native methods can not be abstract.
  4. Native methods can throw exceptions.
  5. Native method is like an abstract method. The implementation of the abstract class and native method exist some where else, other than the class in which the method is declared.
Synchronized : access modifier
  1. Synchronized keyword can be applied to methods or parts of the methods only.
  2. Synchronize keyword is used to control the access to critical code in multi-threaded programming.



    Continue Reading: 
    Declaration of access specifier and access modifiers
      Read:  Java OOPs concepts


                                                           Java Source World                   A Java quick reference blog

      No comments:

      Post a Comment