0% found this document useful (0 votes)
31 views7 pages

Access, Encapsulation, and Static Methods Cheatsheet

Static methods and variables in Java are associated with the class rather than objects of the class. They are declared using the static keyword and can be accessed using the class name without needing an instance of the class. Static methods cannot access instance variables but both static and non-static methods can access static variables. Encapsulation in Java involves making instance variables private and using public getter and setter methods to access them, preventing direct access from outside the class.

Uploaded by

Samy Mehdid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views7 pages

Access, Encapsulation, and Static Methods Cheatsheet

Static methods and variables in Java are associated with the class rather than objects of the class. They are declared using the static keyword and can be accessed using the class name without needing an instance of the class. Static methods cannot access instance variables but both static and non-static methods can access static variables. Encapsulation in Java involves making instance variables private and using public getter and setter methods to access them, preventing direct access from outside the class.

Uploaded by

Samy Mehdid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Cheatsheets / Learn Java

Access, Encapsulation, and Static


Methods

The public and private keywords


In Java, the keywords public and
private define 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.

The private Keyword


In Java, instance variables are encapsulated by public class CheckingAccount{
using the private keyword. This prevents
// Three private instance variables
other classes 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
private int balance;
access to that value stored in that variable.
without having direct access to the variable
itself. //An accessor method
Accessor methods take no parameters and have
public int getBalance(){
a return type that matches the type of the
variable they 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
private int balance;
the ability to modify the value stored in that
variable without having direct access to the
variable itself. //A mutator method
Mutator methods take one parameter whose
public void setBalance(int
type matches the type of the variable it is
modifying. Mutator methods usually don’t return newBalance){
anything. this.balance = newBalance;
}
}

Local Variables
In Java, local variables can only be used within public void exampleMethod(int
the scope that they were defined in. This scope
exampleVariable){
is often defined by a set of curly brackets.
Variables can’t be used outside of those // exampleVariable can only be used
brackets. inside these curly brackets.
}
The this Keyword with Variables
In Java, the this keyword can be used to public class Dog{
designate the difference between instance
public String name;
variables and local 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 public class ExampleClass{
methods when writing classes.
public void exampleMethodOne(){
System.out.println("Hello");
}

public void exampleMethodTwo(){


//Calling a method using this.
this.exampleMethodOne();
System.out.println("There");
}
}
Static Methods
Static methods are methods that can be called // static method
within a program without creating an object of
public static int getTotal(int a, int
the class.
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 int largerNumber = Math.max(3, 10); //
dot operator to a class name followed by the
Call static method
name of the 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
Prints: 7
that can 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
The static Keyword
Static methods and variables are declared as public class ATM{
static by using the static keyword upon
// Static variables
declaration.
public static int totalMoney = 0;
public static int numATMs = 0;

// A static method
public static void averageMoney(){
System.out.println(totalMoney /
numATMs);
}

Static Methods and Variables


Static methods and variables are associated with public class ATM{
the class as a whole, not objects of the class.
// Static variables
Both are used 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();
}

}
Static Methods with Instance Variables
Static methods cannot access or change the class ATM{
values of 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 class ATM{
or 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;
}
}
Static Methods and the this Keyword
Static methods do not have a this reference public class DemoClass{
and are 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()"
}
}

Print Share

You might also like