Week 4 Part 2 Flashcards
T/F - We declare the main method static so that the JVM can invoke ClassName.Main() without creating an instance of the class
True
T/F - Static variables can only be used in some of the classes methods
False. They can be used in all them as they have “class scope”.
ClassName.StaticMethod() - Does this work?
Yes
How do private static class members differ from public ones?
They can only be accessed by through the methods of the class
Can static methods access a class’ instance variables and instance methods?
No. Because static methods exist before any objects of the class have been instantiated.
Can the “this” reference be used in a static method? Why?
No. Because static methods are loaded before anything else. There’s no “this.var” to grab
T/F - A static variable is given a default value if it is not initalized
True
A static method calls an instance method of the same class by using only the method name, what happens?
Compilation error
A static method attempts to access an instance variable in the same class by using only the variable name, what happens?
Compilation error
What can a static method directly relate to?
Other static members that have been loaded with the class
Can instance methods relate to static methods? how about other instance methods?
They can relate to both.
public class JustTheStatic {
public String me = “Mine”;
public static void main(String[] args){
System.out.println(“Now” + me); }
What happens here? Does it run?
It does not run. Despite being a part of a static class “me” is not static. Therefore when main attempts to run “me” is not yet in memory.
T/F - Static objects in Java are immutable
False
T/F - String objects are immutable
True. (Cannot be modified after they are created)
When we concatenate two strings together what happens? (Hint - immutable)
A new string is created containing the concatenated values. Original string values are not modified.
Typically, the garbage collector _____ eventually reclaim the memory for any objects that are eligible
MIGHT
T/F - The JVM guarantees the garbage collector will execute, at the right time
False
T/F - The garbage collector will collect each eligible object when the time comes
False. May collect no objects or simple a subset of eligible ones
Principle of least privilege - Is it important? What is it?
It’s fundamental to good software engineering. It suggests that code should only be granted enough privileges to accomplish its designated tasks.
How does the principle of least privilege make your code better?
Prevents code from accidentally modifying variable values and calling methods that should not be accessible.
final variables can be initialized when they are declared or by each __________, so that each object of the class has a different value
constructor
If a class with final vars has multiple constructors, what would each one be required to do?
Initialize each final variable (assuming it’s not done by the class)
What happens if a final variable is not initalized?
A compilation error
When a class has references to objects of other classes as members
Composition (has-a relationship)
IE. AlarmClock objects need current time and the time when it is to sound its alarm, therefore it would have 2 references to time objects and in AlarmClock