• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What is MyClass.class?

 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

}
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
MyClass.class is an instance of java.lang.Class. Does that answer your question?
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 389
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Paddy spent all of his days in the O'Furniture back yard with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic