By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,782 Members | 1,154 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,782 IT Pros & Developers. It's quick & easy.

Getting class name without instance?

P: n/a
Is there any way to get the name of a class without an instance (i.e.,
object of the class)? I am working with log4j, and would like a uniform
way to name loggers without typing in the name of the class for each
individual class. For example,

------------
import org.apache.log4j.Logger;

public class Foo {
...

private static Logger logger = Logger.getLogger([get either Class
object or class name here--the result should be the Class Foo.class or
its fully qualified name as a String]);
}

------------
The point is that this same statement needs to be in every class where
logging is done, and I would like them all to be identical.

================================================
Question 2:

A related question is how to get the name and signature of the method
(this time in an object) that a statement appears in. I.e.,

-----------
public class Foo {

public void bar(String s) {
System.out.println("I am now in " + [get method name and
signature--the result should be "bar(String s)" or the equivalent]);
}
}
------------
This second question relates to having the logger record the name of the
active method in a way that is uniform across all classes and methods.

Thanks for your help!

Sibyl

Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Sibyl wrote:

[snip]
import org.apache.log4j.Logger;

public class Foo {
...

private static Logger logger = Logger.getLogger([get either Class
object or class name here--the result should be the Class Foo.class
or its fully qualified name as a String]);
}

------------
The point is that this same statement needs to be in every class
where logging is done, and I would like them all to be identical.

[snip]

Hi,
How about:

java.lang.Class c = Foo.class;

and do whatever you need with the Class object, like getName().

- --
Chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)

iD8DBQE/x98JwxczzJRavJYRAqkOAJ9Lv9qhdkR5ZuSMbDamBcvgLjqIpw CgwsRn
0A0fipDLig1JjhktjKWsReE=
=zLFy
-----END PGP SIGNATURE-----
Jul 17 '05 #2

P: n/a
SMC
On Sat, 29 Nov 2003 18:49:25 +1100, Chris wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Sibyl wrote:

[snip]
import org.apache.log4j.Logger;

public class Foo {
...

private static Logger logger = Logger.getLogger([get either Class
object or class name here--the result should be the Class Foo.class or
its fully qualified name as a String]); }

------------
The point is that this same statement needs to be in every class where
logging is done, and I would like them all to be identical.

[snip]

Hi,
How about:

java.lang.Class c = Foo.class;

and do whatever you need with the Class object, like getName().


Or Class.forName("foo.class")

--
Sean

"There are 10 types of people in this world, those who can count in binary,
and those who can't."
Jul 17 '05 #3

P: n/a
SMC <sm*@nospam.org> wrote in message news:<pa*********************************@nospam.o rg>...
On Sat, 29 Nov 2003 18:49:25 +1100, Chris wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Sibyl wrote:

[snip]
import org.apache.log4j.Logger;

public class Foo {
...

private static Logger logger = Logger.getLogger([get either Class
object or class name here--the result should be the Class Foo.class or
its fully qualified name as a String]); }

------------
The point is that this same statement needs to be in every class where
logging is done, and I would like them all to be identical.

[snip]

Hi,
How about:

java.lang.Class c = Foo.class;

and do whatever you need with the Class object, like getName().


Or Class.forName("foo.class")


That's the way I initialize a log4j-logger :

private static Logger logger = Logger.getLogger(getClass());

Is the same line of code in every class.
Jul 17 '05 #4

P: n/a
Maj0r wrote:
SMC <sm*@nospam.org> wrote in message news:<pa*********************************@nospam.o rg>...
On Sat, 29 Nov 2003 18:49:25 +1100, Chris wrote:

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Sibyl wrote:

------------
The point is that this same statement needs to be in every class where
logging is done, and I would like them all to be identical.

[snip]

Hi,
How about:

java.lang.Class c = Foo.class;

and do whatever you need with the Class object, like getName().


Or Class.forName("foo.class")

That's the way I initialize a log4j-logger :

private static Logger logger = Logger.getLogger(getClass());

Is the same line of code in every class.


Am I missing something? Object.getClass() is not static and the above
code would not compile.

Ray

Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.