File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

creating object

kaushik saha

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

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


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

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.


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: 1090
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.
I agree. Here's the link:
subject: creating object
It's not a secret anymore!