+ 1
Usually variables are not assigned in classes. If any variable will be assigned at all, it will be assigned in the "main()". Your question highlights a very import concept, i.e the use of setters against the use of constructors. For almost every application or program that you can think about, users interact with the program. When users interact with the program and enter/type their own data it is applied with a setter. Therefore using a setter implies that you expect input from the user for that particular attribute. Use of a constructor implies the existence of some default variables which the programmer incorporates into the program. A very good example is the 'Vehicle class'. There is an 'int wheels' attribute which can be set to 4 using the constructor. This means throughout the program we have restricted the wheels attribute to 4. Why would you use one or another method of assigning? It will depend on the design of your program. If you want complete interactivity without setting some default values s
8th Sep 2016, 4:18 PM
Mubarak
Mubarak - avatar