Object-Oriented Java Cheatsheet
Object-Oriented Java Cheatsheet
Object-Oriented Java
// behavior of an object
public void set_value() {
age = 20;
name = "Robin";
}
public void get_value() {
System.out.println("Age is " +
age);
System.out.println("Name is " +
name);
}
// main method
public static void main(String []
args) {
// creates a new Person object
Person p = new Person();
Constructor Signatures
A class can contain multiple constructors as long as // The signature is `Cat(String
they have di"erent parameter values. A signature
furLength, boolean hasClaws)`.
helps the compiler di"erentiate between the
di"erent constructors. public class Cat {
A signature is made up of the constructor’s name String furType;
and a list of its parameters.
boolean containsClaws;
Declaring a Method
Method declarations should de!ne the following // Here is a public method named sum
method information: scope (private or public), return
type, method name, and any parameters it receives.
whose return type is int and has two
int parameters a and b
public int sum(int a, int b) {
return(a + b);
}
Print Share