Firefox about:srcdoc
Cheatsheets / Learn Java
Access, Encapsulation, and Static
Methods
The static Keyword
Static methods and variables are declared as static by public class ATM{
using the static keyword upon declaration.
// Static variables
public static int totalMoney = 0;
public static int numATMs = 0;
// A static method
public static void averageMoney(){
System.out.println(totalMoney /
numATMs);
}
1 of 8 7/3/25, 18:11
Firefox about:srcdoc
Static Methods and Variables
Static methods and variables are associated with the public class ATM{
class as a whole, not objects of the class. Both are used
// Static variables
by using the name of the class followed by the .
operator. public static int totalMoney = 0;
public static int numATMs = 0;
// A static method
public static void averageMoney(){
System.out.println(totalMoney /
numATMs);
}
public static void main(String[] args){
//Accessing a static variable
System.out.println("Total number of
ATMs: " + ATM.numATMs);
// Calling a static method
ATM.averageMoney();
}
2 of 8 7/3/25, 18:11
Firefox about:srcdoc
Static Methods with Instance Variables
Static methods cannot access or change the values of class ATM{
instance variables.
// Static variables
public static int totalMoney = 0;
public static int numATMs = 0;
public int money = 1;
// A static method
public static void averageMoney(){
// Can not use this.money here
because a static method can't access
instance variables
}
Methods with Static Variables
Both non-static and static methods can access or class ATM{
change the values of static variables.
// Static variables
public static int totalMoney = 0;
public static int numATMs = 0;
public int money = 1;
// A static method interacting with a
static variable
public static void staticMethod(){
totalMoney += 1;
}
// A non-static method interactingwith
a static variable
public void nonStaticMethod(){
totalMoney += 1;
}
}
3 of 8 7/3/25, 18:11
Firefox about:srcdoc
Static Methods and the this Keyword
Static methods do not have a this reference and are public class DemoClass{
therefore unable to use the class’s instance variables or
call non-static methods.
public int demoVariable = 5;
public void demoNonStaticMethod(){
}
public static void demoStaticMethod(){
// Can't use "this.demoVariable" or
"this.demoNonStaticMethod()"
}
}
The public and private keywords
In Java, the keywords public and private de�ne
the access of classes, instance variables, constructors,
and methods.
private restricts access to only the class that
declared the structure, while public allows for
access from any class.
Encapsulation
Encapsulation is a technique used to keep
implementation details hidden from other classes. Its
aim is to create small bundles of logic.
4 of 8 7/3/25, 18:11
Firefox about:srcdoc
The private Keyword
In Java, instance variables are encapsulated by using public class CheckingAccount{
the private keyword. This prevents other classes
// Three private instance variables
from directly accessing these variables.
private String name;
private int balance;
private String id;
}
Accessor Methods
In Java, accessor methods return the value of a public class CheckingAccount{
private variable. This gives other classes access to
private int balance;
that value stored in that variable. without having direct
access to the variable itself.
Accessor methods take no parameters and have a //An accessor method
return type that matches the type of the variable they
public int getBalance(){
are accessing.
return this.balance;
}
}
Mutator Methods
In Java, mutator methods reset the value of a public class CheckingAccount{
private variable. This gives other classes the ability
private int balance;
to modify the value stored in that variable without
having direct access to the variable itself.
Mutator methods take one parameter whose type //A mutator method
matches the type of the variable it is modifying. Mutator
public void setBalance(int newBalance){
methods usually don’t return anything.
this.balance = newBalance;
}
}
5 of 8 7/3/25, 18:11
Firefox about:srcdoc
Local Variables
In Java, local variables can only be used within the public void exampleMethod(int
scope that they were de�ned in. This scope is often
exampleVariable){
de�ned by a set of curly brackets. Variables can’t be
used outside of those brackets. // exampleVariable can only be used
inside these curly brackets.
}
The this Keyword with Variables
In Java, the this keyword can be used to designate public class Dog{
the di�erence between instance variables and local
public String name;
variables. Variables with this. reference an instance
variable.
public void speak(String name){
// Prints the instance variable named
name
System.out.println(this.name);
// Prints the local variable named
name
System.out.println(name);
}
}
The this Keyword with Methods
In Java, the this keyword can be used to call methods public class ExampleClass{
when writing classes.
public void exampleMethodOne(){
System.out.println("Hello");
}
public void exampleMethodTwo(){
//Calling a method using this.
this.exampleMethodOne();
System.out.println("There");
}
}
6 of 8 7/3/25, 18:11
Firefox about:srcdoc
Static Methods
Static methods are methods that can be called within a // static method
program without creating an object of the class.
public static int getTotal(int a, int b)
{
return a + b;
}
public static void main(String[] args) {
int x = 3;
int y = 2;
System.out.println(getTotal(x,y)); //
Prints: 5
}
Calling a Static Method
Static methods can be called by appending the dot int largerNumber = Math.max(3, 10); //
operator to a class name followed by the name of the
Call static method
method.
System.out.println(largerNumber); //
Prints: 10
The Math Class
The Math class (which is part of the java.lang System.out.println(Math.abs(-7.0)); //
package) contains a variety of static methods that can
Prints: 7
be used to perform numerical calculations.
System.out.println(Math.pow(5, 3)); //
Prints: 125.0
System.out.println(Math.sqrt(52)); //
Prints: 7.211102550927978
Print Share
7 of 8 7/3/25, 18:11
Firefox about:srcdoc
8 of 8 7/3/25, 18:11
Firefox about:srcdoc
Cheatsheets / Learn Java
Inheritance and Polymorphism
Inheritance in Java
Inheritance is an important feature of object-oriented // Parent Class
programming in Java. It allows for one class (child class)
class Animal {
to inherit the �elds and methods of another class
(parent class). For instance, we might want a child class // Animal class members
Dog to inherent traits from a more general parent }
class Animal .
When de�ning a child class in Java, we use the keyword
// Child Class
extends to inherit from a parent class.
class Dog extends Animal {
// Dog inherits traits from Animal
// additional Dog class members
}
Main() method in Java
In simple Java programs, you may work with just one // Shape.java file
class and one �le. However, as your programs become
class Shape {
more complex you will work with multiple classes, each
of which requires its own �le. Only one of these �les in public static void main(String[] args)
the Java package requires a main() method, and this {
is the �le that will be run in the package.
Square sq = new Square();
For example, say we have two �les in our Java package
for two di�erent classes: }
• Shape , the parent class. }
• Square , the child class.
If the Java �le containing our Shape class is the only
// Square.java file
one with a main() method, this is the �le that will be
run for our Java package.
class Square extends Shape {
1 of 5 7/3/25, 18:11
Firefox about:srcdoc
super() in Java
In Java, a child class inherits its parent’s �elds and // Parent class
methods, meaning it also inherits the parent’s
class Animal {
constructor. Sometimes we may want to modify the
constructor, in which case we can use the super() String sound;
method, which acts like the parent constructor inside Animal(String snd) {
the child class constructor.
this.sound = snd;
Alternatively, we can also completely override a parent
class constructor by writing a new constructor for the }
child class. }
// Child class
class Dog extends Animal {
// super() method can act like the
parent constructor inside the child class
constructor.
Dog() {
super("woof");
}
// alternatively, we can override the
constructor completely by defining a new
constructor.
Dog() {
this.sound = "woof";
}
}
Protected and Final keywords in Java
When creating classes in Java, sometimes we may want class Student {
to control child class access to parent class members.
protected double gpa;
We can use the protected and final keywords to
do just that. // any child class of Student can
protected keeps a parent class member accessible access gpa
to its child classes, to �les within its own package, and
by subclasses of this class in another package.
final protected boolean isStudent() {
Adding final before a parent class method’s access
modi�er makes it so that any child classes cannot return true;
modify that method - it is immutable. }
// any child class of Student cannot
modify isStudent()
}
2 of 5 7/3/25, 18:11
Firefox about:srcdoc
Polymorphism in Java
Java incorporates the object-oriented programming // Parent class
principle of polymorphism.
class Animal {
Polymorphism allows a child class to share the
information and behavior of its parent class while also public void greeting() {
incorporating its own functionality. This allows for the System.out.println("The animal greets
bene�ts of simpli�ed syntax and reduced cognitive
you.");
overload for developers.
}
}
// Child class
class Cat extends Animal {
public void greeting() {
System.out.println("The cat meows.");
}
}
class MainClass {
public static void main(String[] args)
{
Animal animal1 = new Animal(); //
Animal object
Animal cat1 = new Cat(); // Cat
object
animal1.greeting(); // prints "The
animal greets you."
cat1.greeting(); // prints "The cat
meows."
}
}
3 of 5 7/3/25, 18:11
Firefox about:srcdoc
Method Overriding in Java
In Java, we can easily override parent class methods in // Parent class
a child class. Overriding a method is useful when we
class Animal {
want our child class method to have the same name as
a parent class method but behave a bit di�erently. public void eating() {
In order to override a parent class method in a child System.out.println("The animal is
class, we need to make sure that the child class method
eating.");
has the following in common with its parent class
method: }
• Method name }
• Return type
• Number and type of parameters
// Child class
Additionally, we should include the @Override
keyword above our child class method to indicate to class Dog extends Animal {
the compiler that we want to override a method in the // Dog's eating method overrides
parent class.
Animal's eating method
@Override
public void eating() {
System.out.println("The dog is
eating.");
}
}
4 of 5 7/3/25, 18:11
Firefox about:srcdoc
Child Classes in Arrays and ArrayLists
In Java, polymorphism allows us to put instances of // Animal parent class with child classes
di�erent classes that share a parent class together in an
Cat, Dog, and Pig.
array or ArrayList .
For example, if we have an Animal parent class with
Animal cat1, dog1, pig1;
child classes Cat , Dog , and Pig we can set up an
array with instances of each animal and then iterate cat1 = new Cat();
through the list of animals to perform the same action
dog1 = new Dog();
on each.
pig1 = new Pig();
// Set up an array with instances of each
animal
Animal[] animals = {cat1, dog1, pig1};
// Iterate through the list of animals
and perform the same action with each
for (Animal animal : animals) {
animal.sound();
Print Share
5 of 5 7/3/25, 18:11