wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "object" Watch "object" New topic


shankaranand jha

Joined: Apr 03, 2007
Posts: 1
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.

Shankaranand jha
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
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();
try {
}catch(Exception e) {

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

Thanks and Regards,

Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
String s = "Howdy!"

Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Sep 03, 2004
Posts: 127
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!"});

J2EE Architect/Developer
It is sorta covered in the JavaRanch Style Guide.
subject: object
Similar Threads
A contradiction....(Kahlid Mughal & Sun Java Tutorial)
What does domain objects in Java mean?
please tel me how the object reference get to know that object is that type?
Object properties
Can we create Object class?