• 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

class Class and class literals

 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Im a little confused about these. in particular the methods Class.forName() Class.newInstance() Object.clone() and class literals. It seems to have something to do with not knowing the exact type when you want to instantiate an class right?
Correct me if i am wrong. Class.forName(Integer) and Integer.class create not an Integer object but a reference to the Class object associated with the Integer class?? What good is the clone method why not just use new.
 
Enthuware Software Support
Posts: 4818
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>Im a little confused about these. in particular the methods Class.forName() Class.newInstance() Object.clone() and class literals. It seems to have something to do with not knowing the exact type when you want to instantiate an class right?
Yes. basically, you can get the parameter (which would be the name of a class) to these methods at RunTime and so can instatiate that class without knowing it's name at compile time.

>Correct me if i am wrong. Class.forName(Integer) and Integer.class create not an Integer object but a reference to the Class object associated with the Integer class??
Yes.

>What good is the clone method why not just use new.
Well, that depends on how much state information your object holds. For. eg. You instantiated an object (using new) say 10 LOCs before and you called a few computation intensive methods on it and now, the object contains a lot of generated data.
If you want to create a copy (clone?) of the same object then either you can new the class and call all the same computation intensive methods (& probab. wait for 5 mins) OR just clone it!
Do see see the point?
HTH,
Paul.

------------------
Get Certified, Guaranteed!
(Now Revised for the new Pattern)
www.enthuware.com/jqplus
 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks. I understand better now.
 
It's weird that we cook bacon and bake cookies. Eat 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