Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

toString override not implemented correctly and giving unexpected output  RSS feed

 
Ranch Hand
Posts: 138
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please consider the following class:



As you can see, in my main() method, I am instantiating a Flight object by passing 175 into the constructor. This is using my first constructor which takes an int value.
However, my overridden toString() method is skipping the return statement immediately after the if condition and returning the else condition, instead.  Since I never passed a "flight class"
the output is:

"My Flight is Flight Class," followed by an aberrant ascii symbol.
 
Rancher
Posts: 2862
32
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

passing 175 into the constructor


What is the constructor doing with that value?

an aberrant ascii symbol.


That is probably the char variable's default value of 0.

If you don't assign any values to the class variables, their values will be the defaults.
 
Mark Richardson
Ranch Hand
Posts: 138
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah! Silly mistake!  Fixed by doing a this.flightClass = flightClass; in the constructor. Thank you.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!