• Post Reply Bookmark Topic Watch Topic
  • New Topic

regarding defalut values  RSS feed

 
Puneet N Vyas
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class instance varibles have default values where as class methods variable doesn't why so??
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should see the assigning of default values to member variables as the exception, not the other way around. Probably the designers of the Java language made it so that member variables get assigned default values because otherwise code would become messy - if you would always need to explicitly supply values to initialise member variables, that would possibly add a lot of code to the class.

Not everything in the Java language has a deep and logical reason. The designers of Java had to make choices, and this is just one choice they made.
 
Puneet N Vyas
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a lot for support
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jesper Young:
if you would always need to explicitly supply values to initialise member variables, that would possibly add a lot of code to the class.

To get the exact same behaviour, just 4, 6 or 8 extra characters per variable; " = 0", " = null" or " = false", depending on the variable type.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!