4 General Flashcards

1
Q

T or F The default constructor is always available.

A

False. Default constructor is NOT available if any other constructor exists in that class. Default is only available when there are no constructors.

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

create question from page 200

A
This will not work. Constructors can be called only by writing new before the name of the
constructor. They are not like normal methods that you can just call. What happens if we
stick new before the constructor name?
public Hamster(int weight) {
new Hamster(weight, "brown"); // Compiles but does not do what we want
}
This attempt does compile. It doesn't do what we want, though. When the constructor
with one parameter is called, it creates an object with the default weight and color. It then
constructs a different object with the desired weight and color and ignores the new object.
That's not what we want. We want weight and color set on the object we are trying to
instantiate in the fi rst place.
Java provides a solution: this—yes, the same keyword we used to refer to instance variables.
When this is used as if it were a method, Java calls another constructor on the same
instance of the class.
public Hamster(int weight) {
this(weight, "brown");
}
Success! Now Java calls the constructor that takes two parameters. weight and color get
set on this instance.
this() has one special rule you need to know. If you choose to call it, the this() call
must be the fi rst noncommented statement in the constructor.
3: public Hamster(int weight) {
4: System.out.println("in constructor");
5: // ready to call this
6: this(weight, "brown"); // DOES NOT COMPILE
7: }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

When can FINAL instance variables be assigned a value?

A

In the line where they are declared (final int num = 1;)
OR in the constructor.

final instance variables must be assigned a value exactly once.

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

When can STATIC FINAL instance variables be initialized?

A

Exactly once,
1- in the declaration line
2- or in STATIC initialization blocks.

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

What does a STATIC import do? Why is it needed instead of a regular import?

A

x

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

What happens if a static variable doesn’t get initialized?

A

It will get assigned the default, null for objects or the primitive defaults.

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

Is this OK?

public class RopeSwing {
  private static final String leftRope;
  private static final String rightRope;
  private static final String bench;
  private static final String name = "name";
  static {
  leftRope = "left";
  rightRope = "right";
  }

public static void main(String[] args) {
System.out.println(“Hi”);
}
}

A

No, the FINAL variable “bench” never gets initialized so it won’t compile. Final variables MUST be initialized or there will be a compiler error.

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

What happens if a FINAL variable doesn’t get initialized?

A

Compiler error

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

When must a FINAL variable be initialized?

A

By the time the constructor completes, if there will be a compiler error. The FINAL variable has to be initialized even if it isn’t used.

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