Beware of Static Finals

Beware of Static Finals

Consider this scenario, you define a public static final variable in one java file, and access the variable in other java file. Now, if you change the value of static variable and compile only the changed java file, you’ll get the wrong result. The example below will make it clear(tested on Sun JDK1.2.2 on WinNT).
Let’s have as:

 	public class StaticFinals {		public static final int VALUE = 10;	}

Access VALUE from other file called

 	public class TestStaticFinals {		public static void main	(String args[]){			int staticvalue = StaticFinals.VALUE;			System.out.println(" Value is " + staticvalue );		}	}

Now compile both the files and run TestStaticFinals.class

It will print “Value is 10.”Now change as below:

 	public class StaticFinals {		public static final int VALUE = 20;	}

Then compile only, (because you have changed only this file) and run TestStaticFinals.class.

Still it will print “Value is 10”, even though you are expecting it to return “Value is 20.”

What does that mean? It means your java compiler copies the value of VALUE at the compile time in to TestStaticFinals.class. So whenever you change the value of public static final variables, remember to recompile all dependent files also.

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several