File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

object

 
shankaranand jha
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String s = "Howdy!"


Bu.
 
Keith Lynn
Ranch Hand
Posts: 2399
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Henrique Ordine
Ranch Hand
Posts: 128
  • 0
  • Mark post as helpful
  • send pies
  • 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!"});
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic