Anatomy of a Class
Reading
Please read sections 6.1-6.5 of the Introduction to Programming With Java book. This is an interactive, free online textbook.
Note: You should skip sections 6.3.3, 6.3.4, 6.3.5, and 6.3.6 (6.3.3 to the end of the 6.3 subsection), which are concepts that we will not be using in this course.
Important concepts
- state
- behavior
- scope
- encapsulation
- instance variables (a.k.a “member variables,” “instance data,” or in Java, “fields”)
- visibility, access control
- getters and setters (a.k.a. “accessors and mutators”)
Things to consider (exercises for yourself, not to turn in)
- Every class defines a constructor method (possibly implicitly). What is the purpose of a constructor?
- Assume you are given code for a class called “Button” that represents a button on your screen. The Button constructor method takes four parameters: double xPosition, double yPosition, double width, double height. Write a single line of code to create a Button variable and initialize it at position (100, 200) with a width of 30 and a height of 20.
- Why do we declare instance (member) variables private? If they are private, what do we need to do in order to access them?
Your response
What was particularly interesting, confusing, or exciting to you in this reading?