Learn Java - Object-Oriented Java Cheatsheet - Codecademy PDF
Learn Java - Object-Oriented Java Cheatsheet - Codecademy PDF
Object-Oriented Java
TOPICS
// 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();
https://www.codecademy.com/learn/learn-java/modules/learn-java-object-oriented-java-u/cheatsheet 1/8
27/04/2020 Learn Java: Object-Oriented Java Cheatsheet | Codecademy
Java instance
Java instances are objects that are based on classes. For example, Bob may be an instance of the class Person .
Every instance has access to its own set of variables which are known as instance fields, which are variables declared within the
scope of the instance. Values for instance fields are assigned within the constructor method.
// Constructor method
public Person(int age, String name) {
this.age = age;
this.name = name;
}
https://www.codecademy.com/learn/learn-java/modules/learn-java-object-oriented-java-u/cheatsheet 2/8
27/04/2020 Learn Java: Object-Oriented Java Cheatsheet | Codecademy
public Person(int a) {
age = a;
}
//For example, `i` and `j` variables are available in the `main` method only:
// Here is a public method named sum whose return type is int and has two parameters a and b
public int sum(int a, int b) {
return(a + b);
}
https://www.codecademy.com/learn/learn-java/modules/learn-java-object-oriented-java-u/cheatsheet 7/8
27/04/2020 Learn Java: Object-Oriented Java Cheatsheet | Codecademy
https://www.codecademy.com/learn/learn-java/modules/learn-java-object-oriented-java-u/cheatsheet 8/8