Topic 6.2: Working with Methods and Encapsulation - Apply the static keyword to methods and fields Flashcards

1
Q

How are static fields different from instance fields?

A

Static fields belong to the class itself and are shared among all instances, while instance fields have separate copies for each object (instance) of a class.

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

How do you invoke a static method in Java?

A

to perform this, you use the class name followed by the method name, without creating an instance of the class.
For example, MathUtils.addNumbers(5, 10)

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

What is the concept of
static methods
in Java?

A

These belong to the class itself rather than an instance of the class.

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

What are static methods in Java used for?

A

These are used for utility operations or computations that are independent of any specific object’s state, such as:

  • performing calculations
  • converting values
  • providing common functionality shared among all instances of a class.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

When are static fields initialized?

A

Static fields are initialized when the class is loaded into memory, typically before any objects of the class are created.

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

These belong to the class itself rather than an instance of the class.

A

What is the concept of
static methods
in Java?

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

These are used for utility operations or computations that are independent of any specific object’s state, such as:

  • performing calculations
  • converting values
  • providing common functionality shared among all instances of a class.
A

What are static methods in Java used for?

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

Why are static fields used in Java?

A

Static fields are used to store data or state that is common to all objects of a class or is not specific to any individual instance, such as constants, configuration settings, or global counters.

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

How do you define a static method in Java?

A

syntax for this:

access_modifier static return_type method_name(parameter_list) {
    // method body
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

syntax for this:

access_modifier static return_type method_name(parameter_list) {
    // method body
}
A

How do you define a static method in Java?

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

What is the effect when static fields are modified?

A

in this case the modification will be reflected across all instances of the class

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

in this case the modification will be reflected across all instances of the class

A

What is the effect when static fields are modified?

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

Can static methods access instance variables or invoke instance methods?

A

No, static methods cannot directly access instance variables or invoke instance methods. They can only access other static members directly.

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

How are static fields accessed in Java?

A

Static fields can be accessed using the class name followed by the field name, without the need to create an instance of the class.

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

to perform this, you use the class name followed by the method name, without creating an instance of the class.
For example, MathUtils.addNumbers(5, 10)

A

How do you invoke a static method in Java?

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