ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "James Carman" <jwcar...@usa.net>
Subject RE: [cinjug] Get class name in static context
Date Sat, 24 Mar 2001 17:18:02 GMT
Static methods cannot be overridden, Robert.  They can only be hidden (see
the Java Language Specification section
http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#2287
45).  Maurice, why do you need to find the name of the class in its static
initializer?  A static initializer block is only run when the class it is
declared in is loaded.  Even if you extend a class with a static
initializer, the static initializer in the superclass is not run when the
subclass is loaded (unless the superclass has not been loaded yet).
Consider the following example...

//
// File: StaticInitializerTest.java
//
public class StaticInitializerTest
{
  public static void main(String[] args) throws Exception
  {
    System.out.println( "Loading Super..." );
    Class.forName( "Super" );
    System.out.println( "Loading Sub..." );
    Class.forName( "Sub" );
  }
}

class Super
{
  protected static String getClassName() { return "Super"; };

  static
  {
    System.out.println( "The name of this class is: " + getClassName() );
  }
}

class Sub extends Super
{
  protected static String getClassName() { return "Sub"; }
}

It produces the following output...

Loading Super...
The name of this class is: Super
Loading Sub...

So, you see, the static initializer block is only run one time.
Furthermore, the static initializer does not call Sub's implementation of
the getClassName method while executing; it uses its own.  Therefore, in a
static initializer block, you already know the name of the class.  It is the
class in which the static initializer block is declared.  Hope this helps.

>From a design perspective, I would declare a static final String to
represent your class' name, instead of hard-coding it. :-)

-----Original Message-----
From: owner-cinjug@loki.sdrc.com [mailto:owner-cinjug@loki.sdrc.com]On
Behalf Of Robert K Casto
Sent: Friday, March 23, 2001 4:52 PM
To: Mills, Maurice; cinjug@loki.sdrc.com; Ant-User (E-mail); Jboss
(E-mail)
Subject: RE: [cinjug] Get class name in static context


I think your best bet is to declare a static method
inside the class that returns a string. Subclasses
can override this static method so that you have the
right string coming back depending on which class
you are using. Because the method is static and the
value returned in the method is static, it should
work and be setup at compile time. If this is still
too late for what you want to do, then I don't have
any ideas that could help you.

Robert Casto
CinciJava
robert@cincijava.com
www.cincijava.com


-----Original Message-----
From: owner-cinjug@loki.sdrc.com [mailto:owner-cinjug@loki.sdrc.com]On
Behalf Of Mills, Maurice
Sent: Friday, March 23, 2001 4:16 PM
To: 'cinjug@loki.sdrc.com'; Ant-User (E-mail); Jboss (E-mail)
Subject: [cinjug] Get class name in static context


Sorry, to the Ant and JBoss lists, but the response is quicker from you.

I am trying to get the name of a class during static initialization.
Normally, you do this.getClass().getName(), however getClass() is not static
and therefore can not be called from a method, or initialization.

Does anyone know how to do this?
======================================================================
To find out more about this mailing list including how to unsubscribe,
send the message "info cinjug" to majordomo@kosh.sdrc.com
======================================================================

======================================================================
To find out more about this mailing list including how to unsubscribe,
send the message "info cinjug" to majordomo@kosh.sdrc.com
======================================================================



Mime
View raw message