• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Confusion regarding object creation in Java.

 
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I know there are two ways to create an object in java. They are as below

First way is by setter
1)


other way is by constructor

2)

It is prefered to use second option while creation of the object. I agree with this, because first step involves two step approach while creatinn an object.
My question here is when to use POJO or javabean..as like in the first step....is there any circumstance of using it?

Thanks in advance.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The set methods do not create a new object. They modify an existing object. In the example you gave, only calling a constructor creates a new object.

Creating an object in Beans calls a no-argument constructor to create the object. Its attributes are altered with the setXXX methods.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rahul Ba wrote:
I know there are two ways to create an object in java. They are as below



There is really only one way of generating Objects in Java - and that is with the new operator. (with some exceptions for Strings and autoboxing).

Rahul Ba wrote:
First way is by setter
1)



other way is by constructor

2)

It is prefered to use second option while creation of the object. I agree with this, because first step involves two step approach while creatinn an object.
My question here is when to use POJO or javabean..as like in the first step....is there any circumstance of using it?

Thanks in advance.



I guess you mean there are two ways to assign data to new objects, either through setXXX() methods or by passing the value into the constructor.

The biggest difference between the two methods is what is allowed to happen to the data, once the object is created. If the value is allowed to change, then you should make a setXXXX() method to change the value. If the value is not allowed to change after Object creation then pass the value in to the constructor and don't have a setXXXX() method.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or, as previously pointed out, in the cases where the class must have a nullary constructor.
 
If you like strawberry rhubarb pie, try blueberry rhubarb (bluebarb) pie. And try this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic