Java Programing laungage

Core Java Tutorial

Introduction of Core Java

How To Install JDk and Set of Path

Syntax of java Program

Difference between Java and C/C++

Advantage and Disadvantage of Java

What is Java

Why Java is not Pure Object Oriented Language

Java has Following Features/Characteristics

Limitation of Java Language and Java Internet

Common Misconception about Java

Simple Program of Java

Integrated Development Environment in java

Compile and Run Java Program

Applet and Comments in Java

Tokens in Java

Keywords in Java

Identifier and Variables in Java

Literals/Constants

Data Type in Java

Assignments and Initialization in Java

Operators in Java

Rule of Precedence in Java

Operator on Integer and Separators in Java Programming

Java Control Flow of Statements

If and If-else Selection Statement

Nested If-else and If-else-If Selection Statement

switch case and conditional operator Selection Statement

for and while Loop

do..while and for each Loop

break and labeled break statement

continue and labeled continue statement

return Statement and exit() Method

Escape Sequence for Special Characters and Unicode Code

Constants and Block or Scope

Statement in Java

Conversions between Numeric Types in Java

Import Statement in Java

User Input in Java using Scanner Class

User Input in Java using Console Class

Array in Java

One Dimensional Array

Two Dimensional Array

Two Dimensional Array Program

Command Line Argument in Java

String args Types in Java

Uneven/Jagged array in java

Math Class Function and Constant

Math Class all Function used in a program

Enumerated Types in Java

Object Oriented Programming v/s Procedural Programming

Object Oriented Programming Concepts in Java

Introduction to Class,Object and Method in Java

Class Declaration in Java

Class & Objects in java

Encapsulation in Java

Modifiers/Visibility for a Class or Interrface or member of a Class

Polymorphism in Java

Runtime polymorphism (dynamic binding or method overriding)

adplus-dvertising
What are Usage Modifiers in java ?
Previous Home Next

In java there are some modifiers that are not access modifiers, still they can modify the way a class or a class member is to be used, we call such modifiers the usage modifiers. Following are usage modifiers used in java :

  1. The final Modifier : The final modifier can be applied to a class, a method or a variable. In general if the an element declared final, is a variable, it means that the value of the variable is constant, and cannot be changed. If a class is declared final, it means the class cannot be inherited, and also the final method cannot be overridden. It can be declared as,

    • final int i = 5;
    • final double d = 2.5;

    The above declaration tell us that the values of i and d will remain constant throughout the course of the program and cannot be changed. For understanding the final modifier in terms of classes and methods, consider the following code snippet,

    Example

    
    Java Tutorialslass ShowFinal
    {
     private final int i=10;
     void Java Tutorialsalculate(int i)
    {
    System.out.println("The value of i is:"+i);
    }
    }
    Java Tutorialslass FinalExample
    {
    public static void main(String args[])
    {
     final ShowFinal obj=new ShowFinal();
    //obj.i=7;      //compilation error as value of i is final can't be changed
    obj.Calculate(10);
    }
    }
    
    
  2. The static Modifier : The static modifier can be applied to variables, methods, and a block of code inside a method. The static elements of a class are visible to all the instances of that class. Thus, if one instance of the class makes a change to a static element, all the instances will see that change.

    A static variable belongs to the class, not to a specific instance of the class, therefore, is initialized when the class is loaded. A static variable may be referenced by an instance of the class in which it is declared or by the class name itself. For better understanding, consider the following code snippet,

  3. Example

    
    Java Tutorialslass StaticModifier
    {
     static int static_counter=0;
     int Java Tutorialsounter =0;
     public StaticModifier()
    {
    static_counter++;
    System.out.println("The value of static counter is :"+static_counter);
    Java Tutorialsounter++;
    System.out.println("The value of normal counter is :"+counter);
    }
    }
     Java Tutorialslass StaticModifierExample
    {
    public static void main(String args[])
    {
     StaticModifier obj=new StaticModifier();    
     // static_counter value=1, counter =1
     StaticModifier obj1=new StaticModifier();  
     // static_counter value=2, counter =1
    //Since the modifier of static_counter is static, 
    which is available to all the instances of the class,
    // the change in value of static_counter in any of the
     instance will be reflected in other instances.
    // while the counter value will remain constant whenever 
    any  instance is created as it is not declared static.
    }
    }
    
    

    Just like a static variable, a static method also belongs to the class in which it is defined and not to a specific instance of the class. Therefore, a static method can only access the static members of the class.

    In other words, a method declared static in a class cannot access the non-static variables and methods of the class. Because a static method does not belong to a particular instance of the class in which it is defined, it can be called even before a single instance of the class exists.

    For example, every Java application has a method main(), which is the entry point for the application execution. It is executed without instantiating the class in which it exists. This can be explained by the following example,

    Example

    
    Java Tutorialslass StaticModifierExample2
    {
    static int a=10;
    static int b=20;
      static void sum()
    {
    System.out.println("The value of sum of two numbers is :"+(a+b));
    }
    public static void main(String args[])
    {
     sum();        
     // static method is called before instantiating any object of StaticModifierExample2
    }
    }
    
    
Previous Home Next