Fields and static methods

The calendar object has a method get, which can be used to get individual fields out of the calendar. For example, to get the current year, we go

  $cal get 1
But where did this magic number "1" come from? If you take a look at java.util.Calendar, you will see a collection of "static final" variables named YEAR MONTH, and so on. These are just constant definitions defined within this class. To get the value of the YEAR constant, we can use the java::field command:
  set yearFlag [java::field java.util.Calendar YEAR]
And we would of course write the above code much more maintainably as
  $cal get $yearFlag
Here's an example of the kind of thing that's easy to whip up in Tcl, which prints the static fields in a class:
  proc showStaticFields {class} {
      foreach f [java::info fields -static java.util.Calendar] {
	  puts "$f = [java::field java.util.Calendar $f]"
      }
  }
  showStaticFields java.util.Calendar
Static?
Java adopts a certain amount of C++ terminology. Variables and methods of a class qualified as static are class-wide. Thus, a static variable is shared by all objects in that class, as opposed to a regular variable which is unique to each object. Static methods are methods that are not associated with a particular object, but with the class itself.
Static methods are called with the java::call command. For example, the java.lang.System class is a collection of static methods that provide access to the state of the Java virtual machine. Here's an example of a call that gets the version number using the static getProperty method:
  java::call System getProperty java.version
This will print the version number of the Java machine that you are running, such as 1.1.6.
Importing java.lang
All classes in the java.lang package are imported by default into Tcl Blend. That is why, in the above call, we didn't have to fully qualify the class name, like this:
  java::call java.lang.System getProperty java.version
Classes in any other package require the fully-qualified class name (for example, java.util.GregorianCalendar). Unfortunately, there is no way to tell Tcl Blend to "import" java packages, to save having to always use the full class name.
To query an object or class for its static methods, use java::info. For example:
  java::info methods -static $cal
and:
  java::info methods -static System

Summary of this section