3.9. Summary

I started this chapter by discussing the scope of these variables: local, method parameter, instance, and class. Often these variables’ scopes overlap each other.

I also covered the constructors of a class: the user-defined and default constructors. Java inserts a default constructor in a class that doesn’t define any constructor. You can modify the source code of such a class, add a constructor, and recompile the class. Upon recompilation, the Java compiler removes the automatically generated constructor.

I then covered the subobjective of reading from and writing to object fields. The terms object fields and instance variables have the same meaning and are used interchangeably. You can read from and write to object fields by directly accessing them or by using accessor methods. I also showed you how to apply encapsulation principles to a class and explained why doing so is useful.

Finally, I explained the effect on references and primitives when they’re passed into methods that change their values. When you pass a primitive value to a method, its value never changes for the calling method. When you pass an object reference variable to a method, a change in its value may be reflected in the calling method—if the called method modifies an object field of the object passed to it. If the called method assigns a new object reference to the method argument before modifying the value of its fields, these changes aren’t visible in the calling method.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.79.84