Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"

Martin Valjavec

+ Follow
since Jan 16, 2004
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Martin Valjavec

Originally posted by Nate Mattson:
....and that is one of the first reasons I would want to query environment variables. Knowing if your in test, or in production is critical.

Setting system properties when starting a Java program from a command line is easy using the -D option; the program can query by property name:
Referencing the value of an environment variable from a command line (so you can pass it as system property) is possible in Unix and Windows, but the command line syntax varies.
Unix: java -DthePropertyName=$theEnvVariableName
Windows: java -DthePropertyName=%theEnvVariableName%
Maybe this can help...
15 years ago
Some environments - such as Smalltalk - automatically define another class for each class you define. This is called a metaclass. Your class definition describes the data structure and methods of your class. The metaclass describes the data structure and methods of the class description - yes, the description of your class is also an object: an instance of the corresponding metaclass.
Exactly one such instance of each metaclass exists at runtime. If you add "class instance variables" and "class methods" to your class, what really happens is that fields and instance methods are added to the metaclass instead. I.e. you can change the structure and behavior of your class description.
In Java, all class descriptions look and work the same: They are instances of java.lang.Class. You cannot add any methods or fields there for some class descriptions where you think you need this; you can use the existing ones, though. E.g. getName() or newInstance() or getMethod()...
This is how the literature defines metaclasses in object orientation. Java obviously does not support metaclasses. However, even in Java you have exactly one class description for each loaded class at runtime. And you can do a lot using their methods (getMethod(), getConstructor(), isArray(), getInterfaces() etc... i.e. using Reflection)
You get the class description for an Object's class by calling getClass() on the object. If you know the classname, you can append .class to it to accomplish the same, e.g. String.class, which uses the class loader of the code that contains this expression for locating the class. Static method forName() in java.lang.Class has an overloaded variant where you can pass in the classname (fully qualified!) and the class loader to be used.
By the way: As we mentioned, in Systems having metaclasses, a "class method" is just a normal instance method of a metaclass, can be called on the instance of a metaclass and is dynamically bound and thus supports polymorphism. Java's static methods have often been compared to such class methods but in fact are - as the name reveals - just static methods, i.e. statically bound, non-polymorphic methods; consequently the syntax for calling them requires no receiver object. The only instance methods acting on class descriptions are those of java.lang.Class.
(sidenote: "class instance variables" in Smalltalk are instance variables of metaclasses; you might have guessed that they should be called "class variables" but these are yet another thing and those two should not be confused when working with Smalltalk. But for Java programmers this does, of course, not matter ;-)
15 years ago