Skip to main content

Constants

info

Constants is a special variable whose value can't be modified during the program execution. Constant is also called as final variable.

Synthax:​

[modifier] final <data Type> <variableName> = <value>;

Example:​

final int a = 99;

final String str1 = "Hello World";

final double d = 99.99;

A variable that is declared as final and not initialized is called blank final variable.

Synthax:​

[modifier] final <data Type> <variableName>;
<variableName> = <value>;

Example:​

final int a;
a=99;

If the variable is declared as final and initialized in the next statement, then in the class file, compiler will replace that variable with the actual value.

SUMMARY​

Variables & Constants​

  1. Default value of char is either ASCII - 0 or UNICODE \u0000.
  2. Default value of char is not space.
  3. JVM will not provide default value for Local Variable.
  4. Local variable must be initialized before using.
  5. JVM will provide default value for static Variable.
  6. Static variable can be accessed from Static method (main method).
  7. JVM will provide default value for instance Variable.
  8. Instance variable cannot be accessed directly from Static method (main method).
  9. You can’t declare multiple variables of different data types in single variable declaration statement.
  10. You can declare multiple variables of same data types in a single statement.
  11. You can assign same value to multiple variables in a single statement.
  12. You can declare and initialize multiple variables of same data type in a single statement.
  13. We can’t declare two variables with same name in same scope.
  14. Value of the variable can be changed any number of times during program execution.
  15. Value of the final variable can not be changed.
  16. We can’t use const keyword to declare constant.