• Post Reply Bookmark Topic Watch Topic
  • New Topic

calling methods directly without creating an object first?  RSS feed

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why can i call the “getPassenger()” method directly without creating an object first? in line 57

 
Sheriff
Posts: 11495
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is an implied object reference there, namely, this. That is, the code is really this.getPassenger(). So you do have an object and it happens to be whatever object is being called upon to execute its addPasenger() method.
 
changyu ji
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:There is an implied object reference there, namely, this. That is, the code is really this.getPassenger(). So you do have an object and it happens to be whatever object is being called upon to execute its addPasenger() method.



so equals and the latter is more clear?
 
Junilu Lacar
Sheriff
Posts: 11495
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
changyu ji wrote:... and the later is more clear?

I did not say that. You asked why you are able to call the getPassenger method without first creating an object and I explained that you already have an implied object: the current instance. Whether the latter is clearer, no, in this case I think it's redundant so the former is preferable of the two. However, in the larger context of what you are trying to do, I would not even use the getter method but would access the member variable directly. Using the getter method can lead to surprises later on if you subclass this class.
 
Bartender
Posts: 572
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since the name of the variable is "passengers", I would also suggest that the name of the method should be getPassengers(), not getPassenger(). The latter name implies a singular item rather than an ArrayList.
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is also something wrong with explicitly iterating the List to look whether the passenger is already booked on this flight. Consider the contains() method of List instead of the loop. Or use a Set<Passenger>, or even a Map<SeatNumber, Passenger> as different data structures which don't support duplicates.
 
Sheriff
Posts: 4935
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
changyu ji wrote:

That variable name passengerExists is misleading. Naturally i'd think about passengerAdded, especially when the method is named as it is.
Moreover, that method does more than 1 thing. Hence it should be split to another method. To check if Passenger is in boarding list, if yes - then add it, and return as added.
 
Junilu Lacar
Sheriff
Posts: 11495
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:
changyu ji wrote:

That variable name passengerExists is misleading. Naturally i'd think about passengerAdded, especially when the method is named as it is.
Moreover, that method does more than 1 thing. Hence it should be split to another method. To check if Passenger is in boarding list, if yes - then add it, and return as added.


That implementation itself could be faulty. It will always add a new passenger with the name provided, regardless of whether or not there's already a passenger in the list with the same name. If you take this implementation as being "correct" then the appropriate JavaDocs for this method would be:


The code would need to be modified if this was the intended behavior:


I would actually extract that check out to a different method:

This keeps the details of checking for prior existence of the given passenger out of the story line for addPassenger. So, extract method to get a more composed method.
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:. . . I would actually extract that check out to a different method:
. . .
That might be easier if you have a Map or Set as I suggested earlier. Of course, there is nothing to stop you using several datatypes in the same app.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!