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