• 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

ClassName.class

 
Ranch Hand
Posts: 153
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can please any body explain

1)public class ThredsLetters extends Thread {
public static void main(String[] args) {
System.out.println(ThredsLetters.class);
}
}

what is the class the above statement ?

2)synchronized(Letters.class) is it valid ? if yes what we are synchronizing here ?
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Wait—what's that MyClass.class thing? That's called a class literal. It's a
special feature in the Java language that tells the compiler (who tells the JVM): go
and find me the instance of Class that represents the class called MyClass.


KS&BB page 738

So... class literal...
Now I think you can guess the answer to the second question, too (Hint: it should be in a static method!)

Hope this helps!
 
Malatesh Karabisti
Ranch Hand
Posts: 153
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Valentin, can you please elaborate ?
 
Valentin Musoiu
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hey, Malatesh!

Sorry for being too succint!

ThredsLetters.class is a class literal... Just like 5 is an int literal, like 5.0 is a double literal (surprisingly, there is a class named 'Class' in java.lang package).

Therefore you can print the class literal (just like you can print any object... you get what the toString() method returns in the Class class). You can have a Class variable. In fact, I imagine you can do all sorts of things, but I haven't got the time to experiment, as it is not asked for SCJP


On the second matter, synchronized(Letters.class) is used when you synchronize a static block of code. You probably know that you can synchronize methods and blocks either static or non-static:
- when you synchronize a non-static method, you simply use "synchronize" keyword to synchronize on the current object
- when you synchronize a non-static block, you have to say what object are you synchronizing on [ ex: synchronize(this) ]
Similarly:
- when you synchronize a static method, you simply use "synchronize" keyword to synchronize on the current class
- when you synchronize a static block, you have to say what class are you synchronizing on [ ex: synchronize(MyClass.class) ]

Hope this time I explained it better!

PS: If I said something stupid (hope I didn't, but.. who knows?), please someone correct me!

PPS: I claim the prize for the most 'synchronize' words in a single post :p
 
Malatesh Karabisti
Ranch Hand
Posts: 153
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Valentin , I think you spent good amount of efforts to explain it from bottom, for that you deserve round of applause , It is more clear now, we mostly used only two words “class” and as you said “synchronize” 200 times in a discussion of 1000 words

Thanks for your efforts.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic