Week 2 Flashcards
What is the purpose of the main method and what is it’s syntax?
The main method is the point of insertion for the program and where we will instantiate classes.
Syntax:
public static void main(String args[]) {
};
What is the essential class structure?
- Constants
- Instance variables (fields)
- Constructors
- Accessors and mutators (getters and setters)
- Methods (behaviours, arranged logically)
- Equals method
- Hashcode
- toString
What are the basic properties of a constructor?
- method name is the same as the class name
- no return type NOT EVEN VOID!
- initializes an object by assigning values to all the instance variables
- uses arguments passed as external parameter values
remember, instance variables are declared in the class not in the constructor.
What are the important properties of instance variables?
- they are declared at the top of a class after the CONSTANTS
- initial values for instance variables are assigned in the constructor
- the scope of instance variables is the entire class, all constructors and methods have access to them
- instance variables are private knowledge unless you would like other classes to be able to use them (use “private” visibility modifier)
Key differences between local variables and instance variables?
local:
- declared inside methods
- compatible values must be assigned to them before using them
instance:
- declared inside the class (after CONSTANTS) and outside methods
- assigned values in the constructor
- default values are assigned if the constructor does not explicitly assign values to them
What are the responsibilities of the constructor?
the constructor is responsible for…
- ensuring instance variables are set to valid and logical initial values
What happens if we exclude a constructor from our class? what if we include multiple?
if no constructor…
- Java compiler will generate a default constructor and add it to the .class bytecode file
- the auto-generated default constructor will set all instance variables to their default values
if multiple…
- no default constructor is added
- constructors can call upon each other using the “this” keyword
What are some key properties of a Java object?
- is an instance of a class
- occupies a location in memory
- maintains a state stored in its instance variables
- can be accessed through a reference (object that stores the object’s address)
Briefly summarize the key points of encapsulation and information hiding
encapsulation:
- each class is responsible for exposing and managing its own state
- we put data with the methods that manage it
information hiding:
- wrap objects in a privacy barrier
- users only need to know how to use an object but not how the objects is able to do what it does
What are the visibility modifiers? Explain the scope of each.
- public
- public members can be referenced anywhere within the same package - private
- private members can only be referenced within the same class - protected (will learn later)
- package-default (will learn later)
What are the basic rules for using visibility modifiers with a class?
- INSTANCE VARIABLES MUST ALWAYS BE PRIVATE
- classes and (most) constructors should be PUBLIC
- methods that are part of the public interface (what the user will interact with) should be public, aka, what the class offers as a service/functionality
- methods that are part of private implementation (what the user doesn’t need to know about) should be private
- CONSTANTS may be private or public, depending on context
- static members may be private or public, depending on context
- local variables are implicitly private therefore no visibility modifier is needed for them
When should we use accessors and what is the syntax for accessors (getters)?
when to use…
- access private instance variables, we may write accessors for SOME instance variables
syntax…
public int getValue() {
return this.aValue;
}
usually come before mutators (setters)
When should we use mutators and what is the syntax for mutators (setters)?
when to use…
- change data stored in an object’s instance variables (change the state of an object), we may write mutators for SOME instance variables
syntax…
public void setInstanceVariableTo(<data> newValue) {
oldValue = newValue;
}</data>
usually come after accessors (getters)
What are some important facts to remember about using mutators?
- use of mutators must be restricted
- mutators must contain logic so the values of variables can be set to valid values
- it is correct for mutators to do nothing if incorrect values are passed to it
- mutator methods should only change the implicit argument
- mutators should limit side effects (IO, modifying the params passed to it, modifying static variables, modifying non-local variable)
What is the use of the toString method? What are some of its properties?
- for developers not users
- describe the state of an object
- should contain all the values of instance variables
- a call to println that passes a reference to an object automatically calls that object’s toString method