Ch12 Java Fundmentals Flashcards
Is the following code valid?
private void doSomething() {
final int x;
}
Yes.
we do not need to assign a value when a final variable is declared. The rule is that it must be assigned a value before it can be used.
Is the following code valid?
final StringBuilder s = new StringBuilder();
s.append(“test”);
Yes.
The object reference is constant and never changes. The state of the object, however, can still change.
Is the following code valid?
public class Bear {
static final int age;
{ age = 8; }
}
The code does not compile because the age variable is static, while the initializer is not.
Given the following enum
public enum Season {
WINTER, SPRING, SUMMER, FALL
}
Does the following code throw an exception?
Season s = Season.valueOf(“summer”);
Yes, it will throw an IllegalArgumentException.
It is case sensitive.
Given the following enum
public enum Season {
WINTER, SPRING, SUMMER, FALL
}
How can we loop over the set of enums and print the names?
Using Season.values() method, which returns an array of all the values, and the name() method.
for (Season season : Season.values()) {
System.out.println(season.name());
}
In addition we can print each order number using ordinal().
Does the following code compile?
Season summer = Season.SUMMER;
switch(summer) {
case Season.SUMMER:
// Do something
break;
default:
// Do something
break;
}
It doesn’t because the enum type (Season) is implicitely added by Java in the case statement. For that reason we only have to write the value (SUMMER) instead of Season.SUMMER.
Which of the following are allowed in an Enum object?
constructor
field
method
All of them.
Does the following code compile?
public enum ConstructorEnum {
public ConstructorEnum() { }
}
No.
The constructor of an enum is implicitely private. This is reasonable since an enum can’t be extended and the constructors can be called only within the enum itself
Given the following enum: Season, the value: SUMMER, and the method: print().
How do we call the print method from outside the enum?
Season.SUMMER.print();
When does Java call the constructor of an enum?
When the enum is first called.
A nested class can come in what flavours?
- Inner class
- Static nested class
- Local class
- Anonymous class
What is an inner class?
An inner class is a non-static type defined at the member level of a class.
What is a static nested class?
A static type defined at the member level of a class.
What is a local class?
A class defined within a method body.
What is an anonymous class?
A special case of a local class that does not have a name.
What are the properties of an inner class?
- Can be declared public, protected, package-private or private
- Can extend any class and implement interfaces
- Can be marked abstract or final
- Cannot declare static fields or methods, except for static final fields
- Can access members of the outer class including private members
Given top level class Outer and nested class Inner, how do you instantiate a nested class from outside the Outer class?
Outer.Inner inner = new Outer().new Inner();
or:
Outer outer = new Outer();
Inner inner = outer.new Inner();
If Inner and Outer both define a variable x, how does inner acces both variables?
- For x in Inner: x or this.x
- For x in Outer: Outer.this.x
What is the difference between a nested class and a static nested class?
For a static nested class, you do not need an instance of the enclosing class to access it (it acts like a static member field).