Chapter 12: Java fundamentals 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.
What does final mean when applied to a method?
The method cannot be overridden.
What does final mean when applied to a reference variable?
The variable cannot be assigned more than once. The object associated with it can still be modified.
What does final mean when applied to a class?
The class cannot be extended.
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().
Is it possible to extend an enum?
No, an enum cannot be extended.
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.
page 503
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 stype 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.
Wat 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).
Can local classes access all local variables?
No, they can only access final or effectively final variables.
Which access modifiers are allowed for a local class?
None.
What is this type of class called?
SaleTodayOnly sale = new SaleTodayOnly() { int dollarsOff() { return 3; } };
An anonymous class