Syntax Flashcards
Static variable
private static int n;
instance variable
private int n;
Static method
public static int calc(int a, int b) {
return a + b * 2;
}
Typecast
BankAccount myAccount = (BankAccount) anObject;
Instanceof
if (anObject instanceof BankAccount)
Wrapper
String input = “5”;
int in = Integer.parseInt(input);
make a child class
public class Child extends Parent
default constructor of a child class
super();
abstract class
public abstract class Instructions {stuff}
interface
public interface Moveable {
implements
public class Warrior implements Moveable, Drawable {
enum def
public enum FilingStatus {SINGLE,MARRIED};
File IO
open:
FileReader reader = new FileReader(filename);
read: using scanner
Scanner data = new Scanner( reader ) ;
String line = data.nextLine() ;
try catch
try {
FileReader reader = new FileReader(“input.txt”);
} catch (FileNotFoundException e) {
System.out.println(“File not found: input.txt”);
}
throws
public void loadFile3() throws FileNotFoundException {
FileReader reader = new FileReader(“input.txt”);
}
throw
if (bad stuff I detect)
throw new Exception(“blah”);
public class Student {
protected String name;
private String address;
private int id ;
public boolean isEnrolled(int year) {
// do stuff
}
}
diagram?
Student
#name: String
-address: String
-id: int
—————-
+isEnrolled(year:int) : boolean
public class GamePanel {
public static final int CELL_SIZE = 20;
private int whoseTurn;
public void drawBoard (Graphics g) { }
}
+CELL_SIZE: int = 20
-whoseTurn: int
—————–
+drawBoard(g:graphics)