Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

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: 67618
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
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic