• Post Reply Bookmark Topic Watch Topic
  • New Topic

What is MyClass.class?  RSS feed

 
John McDonald
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi There,

I have a class

public class MyClass
{
//When I do logging I have to do
Logger = Logger.getLogger(MyClass.class);

How do I pass MyClass.class to other class by which type? Thanks.

}
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do not understand. Why do you want to pass a reference of MyClass.class to other classes.



Can you please explain a little more of what you really want to do?
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
MyClass.class is an instance of java.lang.Class. Does that answer your question?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66261
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Edwin Dalorzo:
Why do you want to pass a reference of MyClass.class to other classes.


Because that's what the logging API requires.

The .class member of any class is a reference to the Class instance that describes the class.
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But, Bear, John has already figured that out. He is currently using MyClass.class to create his logger.

Something else:



Because that's what the logging API requires.



Actually the getLogger() method of the java.util.logging API just requires a String name for the logger. Typically we use the name of the class, but that is not a requirement.

What I meant with my question was to ask Jhon what is that he wants to do with MyClass.class that he cannot understand, because I do not understand his question.

I understand what is purpose of passing MyClass.class to the logger, even so I do not get his question.

Do you guys understand?

[ April 14, 2006: Message edited by: Edwin Dalorzo ]
[ April 14, 2006: Message edited by: Edwin Dalorzo ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Edwin Dalorzo:

What I meant with my question was to ask Jhon what is that he wants to do with MyClass.class that he cannot understand, because I do not understand his question.

I understand what is purpose of passing MyClass.class to the logger, even so I do not get his question.


Frankly, I'm not sure I understood the question correctly. I thought that the "by which type" part might have been the crux.
 
Srikanth Raghavan
Ranch Hand
Posts: 389
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have understood the question, but I don't have an answer, cause I have the same question too.

The example he showed


Is to create a Log4j "Logger" instance for that class. Log 4j uses reflection for logging and that's how it finds out the class name and method names. But, I don't have a clear idea though.

But, I don't think that is his question. His question is that what is...



We all know that SomeClass.this represents the currect object. But what is that ".class" that returns an instance of type "java.lang.Class"???
[ April 15, 2006: Message edited by: Srikanth Srinivasa Raghavan ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Srikanth Srinivasa Raghavan:
But what is that ".class" that returns an instance of type "java.lang.Class"???


It's the class literal: http://java.sun.com/developer/TechTips/1997/tt1118.html
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!