Obj 3: Utilizing Java Object-Oriented Approach Flashcards
what is an object ?
An object is a runtime instance of a class
in memory.
what is a reference ?
A reference is a variable that points to an object
what are the members of a class ?
Java classes have two primary elements: methods (often called functions or procedures) and fields (known as variables
Variables hold the state of the program, and methods operate on that state. If the change is important to remember, a variable stores that change
what is a method signature ?
The method name and parameter types are called the method signature
what does the main() method ?
The main() method lets the JVM call our code. The simplest possible class with a main() method looks like this:
public class Zoo {
public static void main(String[] args) {
System.out.println(“Hello World”);
} }
what are packages ?
Java puts classes in packages. These are logical groupings for classes.
import java.util.*
does it import child packages, fields or methods ?
The * is a wildcard that matches all classes in the package
The import statement doesn’t bring in
child packages, fields, or methods; it imports only classes directly under the package
import java.util.Date; import java.sql.Date;
does it compile ?
No, Because there can’t be two defaults, the compiler tells you the imports are
ambiguous.
do this instead
public class Conflicts { java.util.Date date; java.sql.Date sqlDate; }
what is reference type ?
A reference type refers to an object (an instance of a class). Unlike primitive types that hold their values in the memory where the variable is allocated, references do not hold the value of the object they refer to. Instead, a reference “points” to an object by storing the memory address where the object is located, a concept referred to as a pointer.
Park p = new Park();
First you declare the type that you’ll be creating (Park) and give the variable a name (p)
This gives Java a place to store a reference to the object. Then you write new Park() to
actually create the object.
public class Chick { public Chick() { System.out.println("in constructor"); } public class Chick { public void Chick() { } // NOT A CONSTRUCTOR } }
There are two key points to note about the constructor: the name of the constructor
matches the name of the class, and there’s no return type.
The purpose of a constructor is to initialize fields, although you can put any code in there.
String str = new String("hello !"); String str_ = str;
does it compile ?
A value is assigned to a reference in one of two ways:
- A reference can be assigned to another object of the same or compatible type.
- A reference can be assigned to a new object using the new keyword.
what is the order of initialization ?
Fields and instance initializer blocks are run in the order in which they appear in the file.
The constructor runs after all fields and instance initializer blocks have run
which one is decalaring a variable or initializing a variable
~~~
String str;
String str_ = “hello!”;
~~~
A variable is a name for a piece of memory that stores data. When you declare a variable, you need to state the variable type along with giving it a name. Giving a variable a value is called initializing a variable. To initialize a variable, you just type the variable name followed by an equal sign, followed by the desired value.
What is local variable ?
A local variable is a variable defined within a constructor, method, or initializer block
final int y = 10; int x = 20; y = x + 10;
does it compile ?
No, The final keyword can be applied to local variables and is equivalent to declaring constants in other languages
final int[] favoriteNumbers = new int[10]; favoriteNumbers[0] = 10; favoriteNumbers[1] = 20; favoriteNumbers = null;
does it compile ?
No, we can modify the content, or data, in the array. The code does not compile when we try to change the value of the reference
public int do() { int y = 10; int x; int reply = x + y; return reply; }
Does it compile ?
No, Local variables do not have a default value and must be initialized before use.
the compiler will report an error if you try to read an uninitialized value
public void findAnswer(boolean check) { int answer; int otherAnswer; int onlyOneBranch; if (check) { onlyOneBranch = 1; answer = 1; } else { answer = 2; } System.out.println(answer); System.out.println(onlyOneBranch); }
Does it compile ?
No, The onlyOneBranch variable is initialized only if check happens to be true. The compiler knows there is the possibility for check to be false, resulting in uninitialized code, and gives a compiler error.
On the exam, be wary of any local variable that is declared but not initialized in a single line. This is a common place on the exam that could result in a “Does not compile” answer. Be sure to check to make sure it’s initialized before it’s used on the exam.
public class Car { private static String color; private String brand; public static void main(String[] args) { System.out.println(color); System.out.println(brand); }
which one is the class variable and the instance variable ?
does it compile ?
no, compiler throws error “non-static variable brand cannot be referenced from a static context”
you should initialize instance variables
color -> class variable
brand -> instance variable
An instance variable, often called a field, is a value defined within a specific instance of
an object
a class variable is one that is defined on the class level and shared among all instances of the class. It can even be publicly accessible to classes outside the class and doesn’t require an instance to use
public class VarKeyword { var tricky = "Hello"; }
Does it compile ?
No, Local variable type inference works with local variables and not instance variables
public void reassignment() { var number = 7; number = 4; number = "five"; }
Does it compile ?
No, When you type var, you are instructing the compiler to determine the type for you. The compiler looks at the code on the line of the declaration and uses it to infer the type.
In Java, var is still a specific type defined at compile time. It does not change type at runtime.
public void doesThisCompile(boolean check) { var question; question = 1; var answer; if (check) { answer = 2; } else { answer = 3; } System.out.println(answer); }
does this compile ?
No, Since question and answer are not assigned values on the lines where they are defined, the compiler does not know what to make of them
~~~
public void twoTypes() {
int a, var b = 3;
var n = null;
}
```
Does this compile ?
No, All the types declared on a single line must be the same type and share the same declaration.
The compiler is being asked to infer the type of null. This could be any reference type. The only choice the compiler could make is Object. However, that is almost certainly not what the author of the code intended. The designers of Java decided it
would be better not to allow var for null than to have to guess at intent.