473,385 Members | 1,912 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Getting class name without instance?

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
4 8545
-----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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

50
by: Dan Perl | last post by:
There is something with initializing mutable class attributes that I am struggling with. I'll use an example to explain: class Father: attr1=None # this is OK attr2= # this is wrong...
3
by: gry | last post by:
I often find myself wanting an instance attribute that can take on only a few fixed symbolic values. (This is less functionality than an enum, since there are no *numbers* associated with the...
5
by: kuvpatel | last post by:
Hi I want to refer a class called LogEvent, and use one of its methods called WriteMessage without actually having to create an instance of Logevent. I have tried using the word sealed with...
5
by: Michael D. Ober | last post by:
Is there a way in VB 6 (and VB 2005) to get the instance name from a class. For example class MyClass 'some methods and properties public property get VarName() as string VarName =...
20
by: Shawnk | last post by:
I would like to get the class INSTANCE name (not type name) of an 'object'. I can get the object (l_obj_ref.GetType()) and then get the (l_obj_typ.Name) for the class name. I there any way of...
2
by: Andrés G. Aragoneses [ knocte ] | last post by:
Hello. I am creating a class that is derived from one that comes from an auto-generated code from a webservice reference. The base class has certain XML attributes that are inherited in my...
6
by: Adam Atlas | last post by:
Is it possible for an object, in its __init__ method, to find out if it is being assigned to a variable, and if so, what that variable's name is? I can think of some potentially ugly ways of...
33
by: JamesB | last post by:
I am writing a service that monitors when a particular app is started. Works, but I need to get the user who is currently logged in, and of course Environment.UserName returns the service logon...
0
by: buntyindia | last post by:
Hi, I have a very strange problem with my application. I have developed it using Struts. I have a TextBox With Some fixed value in it and on Submit iam passing it to another page. <html:form...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.