Last active
December 19, 2015 12:19
-
-
Save theotherian/5953709 to your computer and use it in GitHub Desktop.
Constants and how they can be misleading
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Sample code for ways Java Constants may not work as you would expect |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class App { | |
| public static void main(String[] args) { | |
| System.out.println(MyAnnotatedClass.class.getAnnotation(MyAnnotation.class).value()); | |
| System.out.println(Constants.OUTPUT_DIRECTORY); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Constants { | |
| // if you're wondering where the drive letter is, you have more reading to do :) | |
| public static final String OUTPUT_DIRECTORY = "/opt/application-output"; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @MyAnnotation(Constants.OUTPUT_DIRECTORY) | |
| public class MyAnnotatedClass { | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.lang.annotation.ElementType; | |
| import java.lang.annotation.Retention; | |
| import java.lang.annotation.RetentionPolicy; | |
| import java.lang.annotation.Target; | |
| @Target(ElementType.TYPE) | |
| @Retention(RetentionPolicy.RUNTIME) | |
| public @interface MyAnnotation { | |
| String value(); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $ tree | |
| . | |
| |____App.java | |
| |____Constants.java | |
| $ javac -cp . *.java | |
| $ tree | |
| . | |
| |____App.class | |
| |____App.java | |
| |____Constants.class | |
| |____Constants.java | |
| $ java -cp . SimpleApp | |
| /opt/application-output | |
| $ rm Constants.class | |
| $ java -cp . SimpleApp | |
| /opt/application-output |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class SimpleApp { | |
| public static void main(String[] args) { | |
| System.out.println(Constants.OUTPUT_DIRECTORY); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment