Friday, February 13, 2015

Java Chapter 4


This chapter explained how instance variables are declared and also initialized. An instance cariable can be declared by typing the data type followed by the variable name. Initializing an instance variable i done through giving the variable an initial value, and that a referenced instance variable will return the default values or null if its a string or 0 if its an integer variable. 



The int variable size was not initialized
This will prevent compiling because it is a local variable
The value of the instance variables determines how the methods behave 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.


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.

 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

Local vs Instance: Local variables are declared within a method, and must be initialized before use, or else it will not compile.Instance variables are declared within a class, but not within a method.










No comments:

Post a Comment