Unit 5: Writing Classes Flashcards
What are the “big three”?
- The instance variables
- Accessor and Mutator methods
- A toString method
Instance Variables
Private variables that are used
Accessor Methods
An instance method that gets the value of a property of an object
Mutator Methods
An instance method that sets the value of a property of an object
What differentiates private and public variables?
A private variable can only be directly accessed from within the class. A public variable can be accessed from outside the class.
Static Variables
private static type name; or, private static type name = value; – Example: private static int theAnswer = 42; static variable: Stored in the class instead of each object. – A "shared" global field that all objects can access and modify. – Like a class constant, except that its value can be changed.
Final Static fields
public static final type name; or, public static final type name = value; – Example: public static final int NUMOFMONTHS = 12; Final static variable: – A class constant whose value cannot be changed. Usually public. – ALL CAPS by convention.
Instance Variables
private type name;
or,
private type name = value;
– Example:
private int id = 243342;
instance variable: Stored in an object instead of the class.
– each object has its own copy of the instance variable.
Static vs Instance Call
A static method is called through the name of the class. An instance method is called through the name of an object. public class Main { public static void main(String[] args) { BankAccount a = new BankAccount("Jim Smith"); //getID is instance // uses object name + dot notation to call System.out.println(a.getID()); //getNumAccounts is static // uses class name + dot notation to call System.out.println(BankAccount.getNumAccounts()); } }
What is the difference between an interface and an abstract class?
(A) There is no difference.
(B) Abstract classes can have methods with bodies (code in them), but interfaces can not.
(C) Abstract classes can be instantiated, while interfaces can not.
(D) Abstract classes can be extended, but interfaces can not.
(E) Abstract classes can declare abstract methods, but interfaces can not.
(B) Abstract classes can have methods with bodies (code in them), but interfaces can not.
Scope
• scope: The part of a program where a variable exists. – From its declaration to the end of the { } braces • A variable declared in a for loop exists only in that loop. • A variable declared in a method exists only in that method. public static void example() { int x = 3; for (int i = 1; i <= 10; i++) { System.out.println(x); } // i no longer exists here } // x ceases to exist here
The “this.” keyword
“this.” : Within a non-static method or a constructor, the keyword
this is a reference to the current object—the object whose
method or constructor is being called.
– Refer to a field: this.field
– Call a method: this.method(parameters);
– One constructor this(parameters);
can call another:
Variable shadowing
• shadowing: 2 variables with same name in same scope. – Normally illegal, except when one variable is a field. public class Point { private int x; private int y; ... public Point(int x, int y) { } – In most of the class, x and y refer to the instance variables. – In the constructor, x and y refer to the method's parameters. The instance variables x and y are global variables.
The parameter
variables x and y
are local variables
Fixing shadowing
"this" can be omitted if it is clear which variable is being referenced. The keyword "this" is helpful to fix the shadowing problem. public class Point { private int x; private int y; public Point(int x, int y) { this.x = x; this.y = y; } } Inside the constructor(or any method with shadowing): – To refer to the data field x, say this.x – To refer to the parameter x, say x
Constructors
One constructor can call another using the this keyword. This helps us reuse another constructor's code. public class Point { private int x; private int y; public Point(int x, int y) { this.x = x; this.y = y; } public Point(){ this(0, 0); } }