Chapter 4 is mainly about behaviors and states and how they relate.
Methods inside classes act differently based on the values of instance variables inside the class. Instance variables can be compared to what the class knows, and methods as what the class does - so, instance variables are the state, and methods the behavior, and the states can affect the behavior. This is seen in the two code snips below.
This code first makes the class Dog, in which the int variable "size" is created. The method "bark" is also defined; this method checks the value of size and changes the string output based on the value. The main class, DogTestDrive, defines three "Dogs", one, two, and three, each of a different size. It then orders the method bark() to be run for each Dog. This shows how the instance variables, or the state, affects the behavior. The instance variable "size" changes the behavior of the code as the size variable is checked and the behavior, bark(), acts based on size
Methods use parameters; a caller passes arguments.
Arguments are passed into methods, and are also parameters - which is a local variable. If a method takes a parameter, it must have something passed to it. If a method is declared to return a value, the return value must be of the declared type, or compatible with the declared type (e.g., int theSecret, int giveSecret) Multiple values can be contained within a parameter, as long as they are separated with a comma. This includes variables, as long as their type is that of the parameter.Encapsulation; encapsulating is important in Java so that variables won't be assigned to something other than what they should be. This is done by marking your instance variables with public and private modifiers. In our dog code above, we can make the instance variable 'size' private just by adding 'private' before 'int.' What is the value of an instance variable before you initialize it? Instance variables will always get a default value; if a value is not assigned, it will default to one of four values, depending on the type.
Int = 0
float = 0.0
boolean = false
ref = null
Methods inside classes act differently based on the values of instance variables inside the class. Instance variables can be compared to what the class knows, and methods as what the class does - so, instance variables are the state, and methods the behavior, and the states can affect the behavior. This is seen in the two code snips below.
This code first makes the class Dog, in which the int variable "size" is created. The method "bark" is also defined; this method checks the value of size and changes the string output based on the value. The main class, DogTestDrive, defines three "Dogs", one, two, and three, each of a different size. It then orders the method bark() to be run for each Dog. This shows how the instance variables, or the state, affects the behavior. The instance variable "size" changes the behavior of the code as the size variable is checked and the behavior, bark(), acts based on size
Methods use parameters; a caller passes arguments.
Arguments are passed into methods, and are also parameters - which is a local variable. If a method takes a parameter, it must have something passed to it. If a method is declared to return a value, the return value must be of the declared type, or compatible with the declared type (e.g., int theSecret, int giveSecret) Multiple values can be contained within a parameter, as long as they are separated with a comma. This includes variables, as long as their type is that of the parameter.Encapsulation; encapsulating is important in Java so that variables won't be assigned to something other than what they should be. This is done by marking your instance variables with public and private modifiers. In our dog code above, we can make the instance variable 'size' private just by adding 'private' before 'int.' What is the value of an instance variable before you initialize it? Instance variables will always get a default value; if a value is not assigned, it will default to one of four values, depending on the type.
Int = 0
float = 0.0
boolean = false
ref = null