Java Flashcards
What is more restrictive - protected or default?
Default (package-private). It allows same package only.
Protected allows other packages if a class in that package extends the class where variable is declared.
What are default values of these instance variables?
~~~
public class A {
String a;
int i;
}
~~~
null, 0
Can we call both methods
and variables
of the parent class using super
?
Yes. Both methods
and variables
.
Pay attention not to call non-static members
from static
.
What would be the output of this class? _______
class B { public B(var s) {} }
Does not compile because var
cannot be used as a method or a constructor parameter.
Can a constructor be static
or/and final
?
No. However it can be private or protected or package-private (default).
How to properly call a constructor from another constructor in the same class?
Using this()
.
The new ClassName()
will compile, but will create another object.
The ClassName()
will not compile.
If neither this()
nor super()
are called from our constructor, what is called by default?
super()
What is Class Initialization order?
First all parent class static variables in the order they appear, then all parent class initializers in the order they appear.
How many times Class initialization happens?
One time at most. It happens on either start of the program or when a static variable is referenced or shortly before an instance of the class is created. Or never if the class never used.
What is initialization order of an object of a class?
Parent class static variables, then static initializers. Child class static variables, then static initializers. Parent class non-static variables, then non-static initializers, then parent constructor. Child class non-static variables, then non-static initializers, then child constructor.
Can we extend the class that defines private constructors only?
No.