MOOC Week 4 Flashcards
What is a java constructor? What do its parametres do?
A special method used to initialise objects, the constructor is called when an object of a class is created. Parametres in a contructor can initialize attributes.
What is static? Why do we use it?
It means the object or method belongs specifically to the class. This is to ensure it cannot be used to access any variables that belong to objects. Also its ensures method implementations cannot be swapped or overide at runtime.
What is enum?
Stands for enumerator. Is is a special data type that allows a variable to be a set of predefined constants
What is the purpose of the this keyword?
Reference to the current object.
What is the purpose of the public keywords.
Access modifiers. Restricts the scope or accessibility of a class, constructor, variables, methods, and data members. It allows JVM to invoke it from outside the class, meaning any object can use the main method.
What is the purpose of the private keyboard and give 2 of its advantages.
Private access modifier that ensures methods, variables and classes can only be accessed in the class in which they were declared or by inheriting parent classes. Private declared methods cannot be override. Private access modifier hides classes from other classes of the same package.
What is the purpose of the JVM?
The engine that drives java code, specifically interpreting byte code into machine code. Java is called platform independent because of byte code. The JVM is responsible for allocating and deallocating memory space.
What is the main method?
A identifier the JVM looks for as the starting point of a program.
What does String[] args mean in the main method?
An array, where every element is at least a string, which will be known as arguments. Meaning it stores command line arguments.
What is the toString() method?
toString() method is used to return the string representation of a object.
When is setVariable naming convention used?
When an methods only purpose is to set a value to the instance variable.
Is a string an object? Can objects be added to a list?
Yes
How to read from file? Example and what packages do you need to import.
import java.util.Scanner;
import java.nio.file.Paths;
public class Read { public static void main(String args[]) { try (Scanner scanner = new Scanner(Paths.get("file1.txt"))) {
while(scanner.hasNextLine()) { String row = scanner.nextLine();
System.out.println(row); } } catch (Exception e) { System.out.print("Error: " + e.getMessage()); }
} }