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.