• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Returning function using instance variable

 
Skanda Raman
Ranch Hand
Posts: 205
Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I have requirement to return different values from a function based on values passed to constructor.

For instance, the values passed to constructor would be opt1,opt2 and opt3. In this case the method test should return respective option values as shown in the program.

I have written the below code, but am not sure if the code could be written still more efficiently by using constructors.

please let me know if you have any suggestions.

 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should use an enum rather than String for your instance variable.

It safer and you can easily use a switch statement.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic