• 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

object

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear everyone
Some one told me that in java there is 5 way of object creation. If anyone have idea about this then pls gives me detail.
bye
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jha,


Some one told me that in java there is 5 way of object creation.



There are two ways an instance of a class can be created

1- Using new operator
ClassName obj = new ClassName();
2-
try {
ClassName.class.newInstance();
}catch(Exception e) {
}

I would also like to know remaining three ways to create objects in java from that Anonymous guy

Thanks and Regards,
cmbhatt
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String s = "Howdy!"


Bu.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is from the Java Language Specification.

A new class instance is explicitly created when evaluation of a class instance creation expression (�15.9) causes a class to be instantiated.
A new class instance may be implicitly created in the following situations:


Loading of a class or interface that contains a String literal (�3.10.5) may create a new String object to represent that literal. (This might not occur if the same String has previously been interned (�3.10.5).)
Execution of an operation that causes boxing conversion (�5.1.7). Boxing conversion may create a new object of a wrapper class associated with one of the primitive types.
Execution of a string concatenation operator (�15.18.1) that is not part of a constant expression sometimes creates a new String object to represent the result. String concatenation operators may also create temporary wrapper objects for a value of a primitive type.


Creation of New Class Instances
 
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If your class has a constructor which takes, for example, a String as a parameter, you could also try:

Constructor cons = YourClass.class.getConstructor(new Class[]{String.class});
YourClass obj = (YourClass) cons.newInstance(new Object[]{"Howdy!"});
 
Well THAT's new! Comfort me, reliable tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic