Learn Java_ Object-Oriented Java Cheatsheet _ Codecademy
Learn Java_ Object-Oriented Java Cheatsheet _ Codecademy
Object-Oriented Java
In Java, instances of a class are known as objects. Every public class Person {
object has state and behavior in the form of instance
// state of an object
fields and methods respectively.
int age;
String name;
// 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
1/17/25, 1:41 PM Learn Java: Object-Oriented Java Cheatsheet | Codecademy
Java instance
Java instances are objects that are based on classes. public class Person {
For example, Bob may be an instance of the class
int age;
Person .
Every instance has access to its own set of variables String name;
which are known as instance fields, which are variables
declared within the scope of the instance. Values for
// Constructor method
instance fields are assigned within the 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
1/17/25, 1:41 PM Learn Java: Object-Oriented Java Cheatsheet | Codecademy
In Java, we use the new keyword followed by a call to public class Person {
the class constructor in order to create a new instance
int age;
of a class.
The constructor can be used to provide initial values to // Constructor:
instance fields. public Person(int a) {
age = a;
}
https://www.codecademy.com/learn/learn-java/modules/learn-java-object-oriented-java-u/cheatsheet 3/8
1/17/25, 1:41 PM Learn Java: Object-Oriented Java Cheatsheet | Codecademy
A variable with a reference data type has a value that public class Cat {
references the memory address of an instance. During
public Cat() {
variable declaration, the class name is used as the
variable’s type. // instructions for creating a Cat
instance
}
Constructor Signatures
https://www.codecademy.com/learn/learn-java/modules/learn-java-object-oriented-java-u/cheatsheet 4/8
1/17/25, 1:41 PM Learn Java: Object-Oriented Java Cheatsheet | Codecademy
null Values
null is a special value that denotes that an object has public class Bear {
a void reference.
String species;
public Bear(String speciesOfBear;) {
species = speciesOfBear;
}
In Java, we use curly brackets {} to enclose the body public class Maths {
of a method.
public static void sum(int a, int b) {
The statements written inside the {} are executed
when a method is called. // Start of sum
int result = a + b;
System.out.println("Sum is " +
result);
} // End of sum
https://www.codecademy.com/learn/learn-java/modules/learn-java-object-oriented-java-u/cheatsheet 5/8
1/17/25, 1:41 PM Learn Java: Object-Oriented Java Cheatsheet | Codecademy
Java variables defined inside a method cannot be used //For example, `i` and `j` variables are
outside the scope of that method.
available in the `main` method only:
https://www.codecademy.com/learn/learn-java/modules/learn-java-object-oriented-java-u/cheatsheet 6/8
1/17/25, 1:41 PM Learn Java: Object-Oriented Java Cheatsheet | Codecademy
A Java method can return any value that can be saved public class Maths {
in a variable. The value returned must match with the
return type specified in the method signature.
The value is returned using the return keyword. // return type is int
public int sum(int a, int b) {
int k;
k = a + b;
Declaring a Method
Method declarations should define the following // Here is a public method named sum
method information: scope (private or public), return
whose return type is int and has two int
type, method name, and any parameters it receives.
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
1/17/25, 1:41 PM Learn Java: Object-Oriented Java Cheatsheet | Codecademy
Print Share
https://www.codecademy.com/learn/learn-java/modules/learn-java-object-oriented-java-u/cheatsheet 8/8