File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes creating object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "creating object" Watch "creating object" New topic
Author

creating object

kaushik saha
Greenhorn

Joined: Sep 22, 2005
Posts: 18
Is it possible in java to create an object without using new keyword.
If it is then how is it possible?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38007
    
  22
More of a beginner's question.

Yes.

Use a factory method.

. . . or something like that.
[ March 20, 2007: Message edited by: Campbell Ritchie ]
Urs Waefler
Ranch Hand

Joined: Mar 13, 2007
Posts: 77
Hi

Have a look at the following source code:


class A {

public static void main (String [] args) {

int [] B = new int [2];
B [0] = 5;
B [1] = 6;

int [] C = {7,8};

}
}


An array is an object. There are two arrays. There is an explicit memory allocation. This is array B. There is an implicit memory allocation. This is array C.

Regards
Urs


SCJP 1.4
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296


That works because the compiler treats Strings specially.


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Do some searching on Java reflection and serialization. Let us know if you find more ways to create object instances.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
You can create objects without using the new operator atleast in the following ways.

1. Reflection
2. Serialization
3. getInstance() method but that would most probably be using the constructor somewhere.
4. Cloning

There might exist other ways also.
 
Consider Paul's rocket mass heater.
 
subject: creating object
 
Similar Threads
Serielizible?
put Data from Vector into DB.
Save a HttpServletResponse
Overloading System.out.println();
servlet