This post originated from an RSS feed registered with Java Buzz
by Goldy Lukka.
Original Post: What is the difference between BLACK and black?
Feed Title: Xyling Java Blogs
Feed URL: http://www.javablogs.xyling.com/thisWeek.rss
Feed Description: Your one stop source for Java Related Resources.
Sounds weird? Well, there is BIG difference in case you are playing with java.awt.Color class.
Recently, in one of my swing applications, I used the following to code to set black as a background color for my textaread: mytextarea.setBackgroud(java.awt.Color.BLACK);
It compiled well but when I gave this application to one of my friend he cried pasting the following stack trace: Exception in thread "main" java.lang.NoSuchFieldError: BLACK at MyClass.initComponents(MyClass.java:74)
He exclaimed saying he has installed the latest JDK (1.5) in his machine.
After a bit of investigation, I came up with the following abstract:
BLACK is a static final field in the class java.awt.Color that represents a "black" color RGB values. The whole issue here is that, till 1.3.x versions, they had a variable called "black" (read case sensitive). May be later in 1.4.x they realized that it is not a standard practice to do so and they define additional variable named "BLACK" to represent the same thing.
Now, when my friend would have tried executing this, his default JRE could not have recognized BLACK because it would be 1.3.x or earlier.
Another question that arises is, if someone installs JDK 1.4.x, why would 1.3.x or older would be his default JRE? The reason is, it could be because of installing any of these: 1. Web browser bundling JRE or earlier versions. 2. Oracle database. 3. Softwares requiring Java and bundling JRE alongwith.
How to see what version of java is default? Simplest. Just open a command prompt and type java -version
Workaround for this? (In windows) 1. Change the registry value. Well, better not play with it. 2. Go to my computer icon and right click. go to advanced tab -> environment variables settings, for all users, set the FIRST entry of path environment variable to your jdk bin directory.