Access, Encapsulation, and Static Methods Flashcards

1
Q

The public and private keywords

A

In Java, the keywords public and private define the access of classes, instances variables, constructors, and methods.

private restricts access to only the class the declared the structure, while public allows for access from any class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Encapsulation

A

Encapsulation is a technique used to keep implementation details hidden from other classes. It’s aim is to create small bundles of logic.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

The private Keyword

A

In Java, instances variables are encapsulated by using the private keyword. This prevents other classes from directly accessing these variables.

EX.

public class CheckingAccount{
//Three private instance variables
private String name;
private int balance;
private String id;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Accessor Methods

A

In Java, accessor methods return the value of a private variable. This gives other classes access to that value stored in that variable. Without having direct access to the variable itself.

Accessor methods take no parameters and have a return type that matches the type of the variable they are accessing.

EX.

public class CheckingAccount{
private int balance;

//An accessor method
public int getBalance() {
return this.balance;
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Mutator Methods

A

In Java, mutator methods reset the value of a private variable. This gives other classes the ability to modify the value stored in that variable without having access to the variable itself.

Mutator methods take one parameter whose type matches the type of the variable it is modifying. Mutator methods usually don’t return anything.

EX.

public class CheckingAccount {
private int balance;

//A mutator method
public void setBalance(int newBalance) {
this.balance = newBalance;
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Local Variables

A

In Java, local variables can only be used within the scope that they were defined in. This scope is often defined by a set of curly brackets. Variables can’t be used outside of those brackets.

EX.

public void exampleMethod(int exampleVariable) {
// exampleVariable can only be used inside these curly brackets.
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

The this Keyword with Varaibles

A

In Java, the this keyword can be used to designate the difference between instance variables and local variables. Variables with this. reference and instance variable.

EX.

public class Dog{
public String name;

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);
} }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

The this Keyword with Methods

A

In Java, the this keyword can be used to call methods when writing classes.

EX.

public class ExampleClass{
public void exampleMethodOne() {
System.out.println(“Hello”);
}

public void examplMethodTwo() {
//Calling a method using this.
this.exampleMethodOne();
System.out.println(“There”);
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Static Methods

A

Static methods are methods that can be called within a program without creating an object of the class.

EX.

//static method
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
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Calling a Static Method

A

Static method can be called by appending the dot operator to a class name followed by the name of the method.

EX.

int largerNumber = Math.max(3, 10); // Call static method
System.out.println(largerNumber); // Prints: 10

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

The Math Class

A

The Math class (which is part of the java.lang package) contains a variety of static methods that can be used to perform numerical calculations.

EX.

System.out.println(Math.abs(-7.0)); //Prints: 7

System.out.println(Math.pow(5, 3)); //Prints: 125.0

System.out.println(Math.sqrt(52)); //Prints 7.21110255927978

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

The static Keyword

A

Static methods are variables are declared as static by using the static keyword upon declaration

EX.

public class ATM {
//Static variables
public static int totalMoney = 0;
public static int numATMs = 0;

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Static Methods and Variables

A

Static methods and variables are associated with the class as a whole, not objects of the class. Both are used by using the name of the class followed by the . operator.

EX.

public class ATM {
//Static variables
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();   } }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Static Methods with Instance Variables

A

Static methods cannot access or change the values of instance varaibles.

EX.

class ATM{
// 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
} }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Methods with Static Variables

A

Both non-static and static methods can access or change the values of static variables.

EX.

class ATM{
//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;
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Static Methods and the this Keyword

A

Static methods do not have a this reference and are therefore unable to use the class’s instance variables or call non-static methods.

EX.

public class DemoClass{

public int demoVariable = 5;

public void demoNonStaticMethod(){

} public static void demoStaticMethod() {
//can’t use “this.demoVariable” or “this.demoNonStaticMethod()”
} }