Java Access Specifiers & Access Modifiers

Java supports both specifiers and modifiers that can dictate the access. An access specifier can be applied to any identifier like variables, methods and classes but modifiers cannot be applied to every identifier, there are limitations. For example, the modifiers applied to variables cannot be used with classes and similarly with methods also. A full discussion of specifiers is given later.
Following list gives the details of access specifiers and modifiers that can be applied to classes, variables and methods.
Class Specifiers & Modifiers
Three modifiers exist that can be applied to classes. Modifier comes just before the class name.
Modifier Keyword Meaning
abstract Objects cannot be created
final cannot be inherited
strictfp Guarantees the same precision for floating-point values in the class irrespective of the OS on which the program is executed
The two access specifiers a class can accept are public or defaultDefault means the specifier is not mentioned at all.
Variable Specifiers & Modifiers
Four modifiers and four specifiers exist that can be applied to variables.
Modifier Keyword Meaning
static Also known as "class variable". No object is necessary to call
final cannot be reassigned
transient Not serialized
volatile Value is more liable to change
All the four access specifiers, Java supports, can be applied to variables – publicprotecteddefault and private. If the specifier is not mentioned, it takes default access. A local variable must be default only.
Method Specifiers & Modifiers
Six modifiers exist that can be used with methods.
Modifier Keyword Meaning
final Subclass cannot override
abstract No method body exists
native Java method takes the help of underlying OS
static Object is not necessary to call
synchronized Used to lock the source and renders a thread-safe operaton
strictfp Guarantees the same precision for floating-point values in the class irrespective of the OS on which the program is executed
All the four access specifiers, publicprotecteddefault and private, can be applied to methods. If not mentioned any access specifier, JVM takes default access.
Read more ...

Can a class be static in Java ?

Have you ever tried to make the top level class static? If you have, then you might have faced some errors. Actually, we can have static class in java. In java, we have static instance variables as well as static methods and also static block. Classes can also be made static in Java.

Java allows us to define a class within another class. Such a class is called a nested class. The class which enclosed nested class is known as Outer class. In java, we can’t make Top level class static.Only nested classes can be static.

What are the differences between static and non-static nested classes?

Following are major differences between static nested class and non-static nested class. Non-static nested class is also called Inner Class.


Nested static class

Non-static nested class

Nested static class doesn’t need reference of Outer class

Non-static nested class or Inner class requires Outer class reference.

A static class cannot access non-static members of the Outer class. It can access only static members of Outer class.

Inner class(or non-static nested class) can access both static and non-static members of Outer class.


/* Java program to demonstrate how to implement static and non-static

   classes in a java program. */

class OuterClass{

   private static String msg = "Way4code.com";

    

   // Static nested class

   public static class NestedStaticClass{

      

       // Only static members of Outer class is directly accessible in nested

       // static class

       public void printMessage() {

 

         // Try making 'message' a non-static variable, there will be

         // compiler error 

         System.out.println("Message from nested static class: " + msg);

       }

    }

    

    // non-static nested class - also called Inner class

    public class InnerClass{

        

       // Both static and non-static members of Outer class are accessible in

       // this Inner class

       public void display(){

          System.out.println("Message from non-static nested class: "+ msg);

       }

    }

}

class Main

{

    // How to create instance of static and non static nested class?

    public static void main(String args[]){

        

       // create instance of nested Static class

       OuterClass.NestedStaticClass printer = new OuterClass.NestedStaticClass();

        

       // call non static method of nested static class

       printer.printMessage();  

  

       // In order to create instance of Inner class we need an Outer class

       // instance. Let us create Outer class instance for creating

       // non-static nested class

       OuterClass outer = new OuterClass();       

       OuterClass.InnerClass inner  = outer.new InnerClass();

        

       // calling non-static method of Inner class

       inner.display();

        

       // we can also combine above steps in one step to create instance of

       // Inner class

       OuterClass.InnerClass innerObject = new OuterClass().new InnerClass();

        

       // similarly we can now call Inner class method

       innerObject.display();

    }

}

Output:

Message from nested static class: Way4code.com
Message from non-static nested class: Way4code.com
Message from non-static nested class: Way4code.com
Read more ...

Google+ Followers

Popular Posts