• Post Reply Bookmark Topic Watch Topic
  • New Topic

How many ways to create instance in Java?  RSS feed

 
Prabhat Ranjan
Ranch Hand
Posts: 397
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My answer is below one.

1) New()
2) Clone()
3) class.forName().newInstance()
4) deserilization of serilized object
5) Factory Method --> using BeanFactory in spring or SessionFactory in Hibernate
6) loading object in web.xml or config file wither in first request or early loading while application startup
I know 6th one might uses the reflection methods similar to class.forName using classLoader.

Please comments any one who is expert and used fully.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question pops up semi-regularly, and I've never understood why it matters. As to the answer, all options but #4 are highly likely to be identical, namely, calling the no-argument constructor.

Can you tell us why you think this question matters?
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prabhat Ranjan wrote:My answer is below one.

1) New()


By 'New()', do you mean the 'new' keyword? Note that n needs to be a small letter and the 'new' keyword is not followed by braces.
You can have a constructor as follows.

Please comments any one who is expert and used fully.

Sorry, I'm not an expert but what kind of comments are you expecting on your answer?
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can think of four more ways. Or maybe they are 3½ ways.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, 4½.
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I can think of four more ways. Or maybe they are 3½ ways.

Campbell Ritchie wrote:No, 4½.


Interesting. One of them, I think, is the enum(s)? Thinking about the other three and a half ways now .. :-)

Edit : But, a half way? Really?
Edit2 : I'm obviously ignoring methods like list.toArray() method, <String>.toCharArray(), and automatic boxing into wrapper types.
 
Prabhat Ranjan
Ranch Hand
Posts: 397
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) new keyword , String str = new String();

1) new
2) clone
3) class.forName().newInstance()
4) deserilization of serilized object
5) Factory Method --> using BeanFactory in spring or SessionFactory in Hibernate
6) loading object in web.xml or config file wither in first request or early loading while application startup

I know 6th one might uses the reflection methods similar to class.forName using classLoader.

Hope this is now fine.

 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still 4½ you haven't mentioned.

Are you sure a Factory Method doesn't involve new Foo() somewhere?
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chan Ag wrote: . . .
Interesting. One of them, I think, is the enum(s)? Thinking about the other three and a half ways now .. :-)
. . .
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doesn't it depend on what you mean by 'create an instance'? for example, de-serializing an object implies the object has already been created. It's like a balloon that has been deflated. Blowing it up doesn't "create" a balloon, does it? Or perhaps a better example is a piece of sheet music. Does the song exist when it's just written down, or does it only exist when it is being performed?

(I do admit I may be getting more philosophical here than most people would)
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I shall let people out of their misery.
  • 1: Boxing. Yes, I think it does count as a way to create an object
  • 2: "Campbell" as a String literal is an object in its own right.
  • 3: The constants in an enum, as Chan Ag said, are objects in their own right.
  • 4: One way to create an array: varargs with the ... operator.
  • 5: Another way to create an array: array initialiser: int[] numbers = {1, 2, 3};
  • You can also create an array with the new operator and an array initialiser: numbers = new int[]{4, 5, 6, 7, 8, 9};
  • But the last method might only be a different form of No 5, so I shall count it as a half.

    And that makes 5½
     
    sai rama krishna
    Ranch Hand
    Posts: 536
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    5=6(the lastone)?
     
    Campbell Ritchie
    Marshal
    Posts: 56546
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    That is why I only put it down as a half.
     
    Wilhelm Vogt
    Greenhorn
    Posts: 10
    Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Just some nitpicking, as already mentioned it does not really matter ...
    Ulf Dittmer wrote:... As to the answer, all options but #4 are highly likely to be identical, namely, calling the no-argument constructor. ...

    I am pretty sure #2 clone() does also not call the no-arg constructor, as clone() also works also if a class implements Cloneable but does not have a no-arg constructor.

    fred rosenberger wrote:Doesn't it depend on what you mean by 'create an instance'? for example, de-serializing an object implies the object has already been created. It's like a balloon that has been deflated. Blowing it up doesn't "create" a balloon, does it? Or perhaps a better example is a piece of sheet music. Does the song exist when it's just written down, or does it only exist when it is being performed?

    I would count a de-serialized instance as new one - it might be created while the original still exists which results in two distinct instances, it might be done several times from the same data and it might be done from input data which is not directly created by serializing an existing object. (the sheet music would be the class, which exists even if there is never an instance of it, and each performance would be an instance - hopefully each time slightly different)

    And to get a whole number of possibilites to create an instance, we might also count as another half way to create an instance.

     
    Chan Ag
    Rancher
    Posts: 1090
    14
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I thought of mentioning the case of instantiating an anonymous implementation of a type also, but we use the 'new' keyword when we do that.
     
    Campbell Ritchie
    Marshal
    Posts: 56546
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Of course you don't need anonymous classes now we have λs. If you remember in the good old days (last year ‍) you created anonymous classes and then you got files called Foo$123 and similar. If you use a λ you don't create such objects and the anonymous class files disappear.
     
    Ulf Dittmer
    Rancher
    Posts: 42972
    73
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I think at this point we have established that the question as asked does not make much sense. So my advice to Prabhat Ranjan is: whatever you intended to do with the answer, you need to rethink why you thought it would be an important thing to know.
     
    Campbell Ritchie
    Marshal
    Posts: 56546
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    It means somebody bet OP a large quantity of beer there weren't more than 5½ ways to create objects
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!