const keyword in java

You are here : Home / Core Java Tutorials /


In this tutorial we will learn about const keyword in java, when to use const keyword in java.


Contents of page >
  • Is const keyword used in java?
  • const keyword was introduced in which java version?
  • What’s the const keyword for?
  • Const in java?
  • const keyword in java and C++


Is const keyword used in java?
NO, const keyword is not used in java.


const keyword was introduced in which java version?
const keyword was introduced in Java 0. i.e. in initial version of java.


What’s the const keyword for?
constant in any programming language means it cannot be altered by the program during execution.
That means the value is constant.


Const in java?
final keyword works same as const in java.
once initialized final variable cannot be assigned a new value in java.

About final variable in detail >
final can be applied to following variable types in java -
  • final memberVariable/instanceVariable
  • final local variable
  • final static variable

Final memberVariable/instanceVariable of class must be initialized at time of declaration, once initialized final memberVariable cannot be assigned a new value in java.


class FinalTest {
   final int x=1; //final memberVariable/instanceVariable
}


If final memberVariable is not declared at time of declaration we will face compilation error= “The blank final field x may not have been initialized”’.


If final memberVariable is assigned a new value we will face compilation error=”The final field FinalTest.x cannot be assigned ”.


If constructor is defined then final memberVariable can be initialized in constructor but  once initialized cannot be assigned a new value.
class FinalTest {
   final int x; //final memberVariable/instanceVariable
   FinalTest() {
          x = 1; //final memberVariable can be initialized in constructor.
   }
}

Final local variable can be left uninitialized at time of declaration and can be initialized later, but once initialized cannot be assigned a new value in java.
class FinalTest {
   void method(){         
          final int x; //final variable uninitialized at time of declaration
          x=1;
   }  
}

Final static variable of class must be initialized at time of declaration or can be initialized in static block, once initialized final static variable cannot be assigned a new value.


If static block is defined then final static variable can be initialized in static block, once initialized final static variable cannot be assigned a new value in java.
class FinalTest {
   final static int x; //final static variable
   static{ //static block
         x=1;
   }
}

So, in short using final with variable means that it cannot be assigned a new value in java.




const keyword in java and C++
Using const in java will produce compilation error


Using const in C++ > In C++ const works same as final in java.
const int x = 1;

Summary -
So, in this core java tutorial we learned Is const keyword used in java? const keyword was introduced in which java version? What’s the const keyword for? Const in java? const keyword in java and C++ .

Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.



RELATED LINKS>

Keywords in java language

No comments:

Post a Comment