Methods and Encapsulation Flashcards

1
Q

Order of method declaration components

A

1) Access Modifier (public, private, etc.)
2) Optional Specifier (final, static, etc.)
3) Return type
4) Method name
5) Parameter list
6) Optional Exception list (throws blahblah)
7) Method body

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

What does the public access modifier mean?

A

The method can be called from any class.

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

What does the private access modifier mean?

A

The method can only be called from within the same class.

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

What does the protected access modifier mean?

A

The method can only be called from classes in the same package or subclasses.

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

What is the default level of access for a method?

A

The method can only be called from classes in the same package.

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

If you are using a vararg parameter, where must it go?

A

At the end of the list of parameters

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

When you omit a vararg parameter from your method call, what does it pass by defualt?

A

An empty array

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

What can you pass in as a vararg parameter?

A

Either an array or a comma separated list of items (that will then be converted to an array) (or null)

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

If a method or variable is static, what does it mean?

A

It’s shared by the whole class basically, so you can call it like ClassName.method() or ClassName.var rather than needing a separate instance of the class. You can also use an instance of the variable to access static things, you just don’t have to. If you set that instance to null, it will still work when accessing static things!

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

Can static methods refer to non-static methods and variables?

A

NO

However a non-static method or variable can reference a static method or variable

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

How do we name static final variables?

A

All caps with underscores between words

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

How can you make all variables declared in a block of code static?

A

static {
int var1 = 1;
int var2 = 2;
}

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

When does the static initializer run?

A

When the class is first used

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

Static Imports

A

Used to import static members of classes so that you don’t have to precede the method o variable name with the class name

import static java.util.Arrays.asList;

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

Is Java pass-by-value or pass-by-reference and what does that mean?

A

It is a pass-by-value language, meaning with primitives a copy of the variable is made and the method receives that copy, so that changes made to the variable passed in are not actually being made to the original value passed in. Rather than passing it the actual reference to the variable, it’s merely using the literal value of it.

With objects however, that copy is still pointing to the same object so the changes will take effect as long as it’s just methods being called on them instead of pointing them to new objects. For example,

StringBuilder s = new StringBuilder();
changeString(s);
void changeString(StringBuilder sb) {
      sb.append("hello"); // makes change
      sb = "howdy"; // Doesn't make change
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Method Overloading

A

When a class contains multiple methods with the same name but different parameter lists.

17
Q

Order in which Java tries to match overloaded method calls

A

1) Exact match by type
2) Larger primitive type (long is larger than int for example)
3) Autoboxed type (Integer as opposed to int)
4) Varargs

18
Q

Default Constructor

A

A constructor without any parameters that is generated by the compiler when no constructors are coded.

19
Q

this()

A

You can use “this” as a method to call a constructor from within another constructor. Note: If you use this() it must be the first non-comment line in the constructor!

public Hamster(int weight) {
  this(weight, "brown");
}
public Hamster(int weight, String color) {     
   this.weight = weight;
   this.color = color;
  }
20
Q

Where are the three places you can initialize a final variable?

A

1) In-line when it is declared
2) In the static initializer block
3) In a constructor

21
Q

Order of Initialization

A

1) If there is a superclass, initialize it first
2) Static variable declarations and static initializers in the order they appear in the file

(Then main method starts)

3) Instance variable declarations and instance initializers in the order they appear in the file
4) The constructor

(p. 202)

22
Q

Encapsulation

A
Combining fields and methods together in a class such that the methods
operate on the data, as opposed to users of the class accessing the fields directly. (Getters and setters, etc.)
23
Q

JavaBean

A

A software component that makes it easy to reuse code.

24
Q

Where is the Predicate interface located?

A

java.util.function

25
Q

Functional Interface

A

An interface where you only implement one method.

These are the only interfaces you can use a lambda expression with

26
Q

How do you access the different elements of a varargs?

A

Just like arrays, so if you want the second element of a varargs called “nums”, then do nums[1]

27
Q

Is this code legal? why?

private static final ArrayList values = new ArrayList<>();
public static void main(String[] args) {
values.add(“changed”);
}

A

Yes, as long you don’t assign a final reference variable to a new object, you can change its qualities

28
Q

How many times can a parameter be converted when trying to find a method it fits with?

A

Once

let’s say there is a method

void number(Long l) {}

then calling number(4) won’t work because it would try to convert to a primitive long then autobox to Long and that is 2 conversions.

calling number(4L) works because it would just require the autoboxing