• Post Reply Bookmark Topic Watch Topic
  • New Topic

Best way to make unique objects  RSS feed

 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys,

what is the best way to create unique objects,with unique instance variables for each object

what i mean is...if i have an Airplane class and in the class i have instance variables.



where is the best place or rather how is the best way to set the instance variables for each unique airplane.

is it best done in a constructor? or are you better off setting it through a method called say designAircraft,that then takes in the arguments that then set the instance variables
or again does it depend on what you want to do...

im sorry if im not explaining it very well...


 
Carey Brown
Saloon Keeper
Posts: 3312
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Best done in a constructor. This leaves open the possibility of having an immutable object, assuming that these parameter won't change over the life of the instance. In which case you may provide getters but not setters (or at least public setters).
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jon ninpoja wrote:. . . is it best done in a constructor? or are you better off setting it through a method called say designAircraft,that then takes in the arguments that then set the instance variables  . . .
If you can, find a copy of Effective Java by Joshua Bloch and read the chapter towards the beginning of the books about object construction. Your designAircraft method sounds like a factory method, and the differences are explained in that book. In the case of an aircraft instance, you may have to initialise every field for every object, in which case it is probably easier to have one constructor and one only.

What do you mean by “unique objects”? Do you mean you never want to have two instances which return true if you compare them using the equals() method?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!