Showing posts with label class level constants. Show all posts
Showing posts with label class level constants. Show all posts

Friday, 21 May 2021

Php: Class level constants

 You can define a class level constant using ‘const’ keyword followed by variable name in capital letters.

 

Example

const ORGANIZATION_NAME = 'ABC Corp';

 

Can I apply access specifier or visibility modifier to class constants?

Yes

 

Example

public const ORGANIZATION_NAME = 'ABC Corp';

 

 

How to refer class constant from outside of the class?

Syntax

ClassName::variable_name

 

Example

Employee::ORGANIZATION_NAME;

 

How to refer the class constant within the class?

Syntax

self::variable_name

 

Example

self::ORGANIZATION_NAME;

 

class_constant_demo.php

#!/usr/bin/php

<?php

    class Employee{
        public const ORGANIZATION_NAME = 'ABC Corp';
        private static $total_employees = 100;

        public static function about_class(){
            $org_name = self::ORGANIZATION_NAME;
            $total_emps = self::$total_employees;

            echo "organization name : $org_name\n";
            echo "total employees : $total_emps\n";
        }

    }

    $org_name = Employee::ORGANIZATION_NAME;
    echo "organization name(outside of class) : $org_name\n";

    Employee::about_class();
   
?>

 

Output

$./class_constant_demo.php 

organization name(outside of class) : ABC Corp
organization name : ABC Corp
total employees : 100

 


  

Previous                                                    Next                                                    Home

Sunday, 9 February 2014

keyword : final

final is used to define constants. The final modifier indicates that the value of this field cannot change.

Defining class level constants
Declare the field with final and static combination

Example
class Person{
 static final int MAX_SALARY = 10000000;
 public static void main(String args[]){ 
  System.out.println(Person.MAX_SALARY);        
 }
}
       
Output
10000000
   
Defining Object level constants
    Declare the field with final keyword.

   Example
class Person{
 final int MAX_SALARY;

 Person(){
  MAX_SALARY = 10000;
 }

 Person(int sal){
  MAX_SALARY = sal;
 }

 public static void main(String args[]){
  Person p1 = new Person(1000000);
  System.out.println(p1.MAX_SALARY);
 }
} 

Output
 1000000 
   
Some Points to Remember
1. You must initialize the static final variables, other wise compiler throws the error
Example
class Person{
 final static int MAX_SALARY;
 public static void main(String args[]){
  System.out.println(Person.MAX_SALARY);
 }
}
  

When you try to compile the above program, compiler throws below error
Person.java:1: error: variable MAX_SALARY might not have been initialized
class Person{
^
1 error

This is not the case with non final variables.
Example
class Person{
 static int MAX_SALARY;
 
 public static void main(String args[]){
  System.out.println(Person.MAX_SALARY);
 }
}

Output    
0
          
2.Static final variables must be initialized at the time of creation or in the static block, Otherwise compiler throws error
class Person{
 static final int MAX_SALARY;
 
 static{
  MAX_SALARY = 1000000;
 }
 
 public static void main(String args[]){
  System.out.println(Person.MAX_SALARY);
 }
}
  
Output
1000000
   

Above program compiles and runs fine, since initializing the static final variable in the static block is completely fine.

3. Trying to initialize the static final variable other than at the time of creation or in static block cause the compile time error. 
Example
class Person{
 static final int MAX_SALARY;
 
 Person(){
  MAX_SALARY = 1000000; //is a static final, can't be initialized here
 }
 
 public static void main(String args[]){
  System.out.println(Person.MAX_SALARY);
 }
}
        
 When you try to compile the above program, compiler throws the below error
Person.java:5: error: cannot assign a value to final variable MAX_SALARY
MAX_SALARY = 1000000;
^
1 error

4. You must initialize the final variables whether those are static or non-static, otherwise compiler throws error
class Person{
   final int MAX_SALARY;
   public static void main(String args[]){
   }
}


When you tries to compile the program, Compiler throws below error
Person.java:1: error: variable MAX_SALARY might not have been initialized
class Person{
^
1 error
                           
5. Final instance variables must be initialized in the constructor or in the instance blocks.
Example 1
class Person{
 final int MAX_SALARY;
 Person(){
  MAX_SALARY = 10000;
 }
}
     
Example 2  
class Person{
 final int MAX_SALARY;
 
 {
  MAX_SALARY = 100000;
 }

 Person(){

 }

 void setSalary(){

 }
}

6. Trying to initialize final instance variables, other than in constructor causes compile time error.  

class Person{
 final int MAX_SALARY;

 Person(){
 }

 void setSalary(){
  MAX_SALARY = 100000;
 }
}
  

Compiler throws the below error
Person.java:8: error: cannot assign a value to final variable MAX_SALARY
MAX_SALARY = 100000;
^
1 error
 
7. final for Reference types: For references to objects, final ensures that the reference will never change, meaning that it always refer to the same object. It makes no guarantees whatsoever about the values inside the object being referred to staying the same.

8. How to declare global constants in Java ?
Make a variable as public static final.
                    


this keyword                                                 nested classes                                                 Home