You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by "Mills, Maurice" <Ma...@CINFIN.com> on 2001/03/23 22:16:05 UTC

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?

RE: [cinjug] Get class name in static context

Posted by Ozzy Espaillat <oz...@unwiredsolutions.com>.
Static methods can not be overridden, so this will not work. It will always
call the static method specified in the signature. i.e.
  Super a = new Sub();
  a.staticMethod();
assuming that both Super and Sub implement
  public static String staticMethod()
then the static method in Super will be called even though the object a is
of type Sub.

I'm not sure what the intent is, maybe you can supply some code to show what
you mean. But if you don't have an object of any kind then you might be
stuck with getting the class name from an outside source i.e. System
property or property file.

Ozzy@unwiredsolutions.com
www.unwiredsolutions.com

-----Original Message-----
From: Robert K Casto [mailto:casto@ee.net]
Sent: Friday, March 23, 2001 1: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
======================================================================



RE: [cinjug] Get class name in static context

Posted by Robert K Casto <ca...@ee.net>.
Thanks for the clarification on the static method
not being able to be overridden.

I have to agree with James though, unless we know
more about what it is you are trying to do, we are
not going to be able to help much with this problem.

Can you give us more details?

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 James Carman
Sent: Saturday, March 24, 2001 12:18 PM
To: Robert K Casto; Mills, Maurice; cinjug@loki.sdrc.com; Ant-User
(E-mail); Jboss (E-mail)
Subject: RE: [cinjug] Get class name in static context


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
======================================================================


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


RE: [cinjug] Get class name in static context

Posted by James Carman <jw...@usa.net>.
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
======================================================================



RE: [cinjug] Get class name in static context

Posted by Robert K Casto <ca...@ee.net>.
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
======================================================================