• Shuffle
    Toggle On
    Toggle Off
  • Alphabetize
    Toggle On
    Toggle Off
  • Front First
    Toggle On
    Toggle Off
  • Both Sides
    Toggle On
    Toggle Off
  • Read
    Toggle On
    Toggle Off
Reading...
Front

Card Range To Study

through

image

Play button

image

Play button

image

Progress

1/28

Click to flip

Use LEFT and RIGHT arrow keys to navigate between flashcards;

Use UP and DOWN arrow keys to flip the card;

H to show hint;

A reads text to speech;

28 Cards in this Set

  • Front
  • Back

How is a static field made?



what can it do and not do?

- private static variable-type userdefined-name = declaration.



- It can be used like a regular field but it will only be created once, it will not be replicated with each instance of the class


- can accept values without an instance ever being created.

How can it be useful?
If you had three objects created from the same class and they each served their own purpose but you wanted to keep a running tally of a certain data from all three of them, you could use a static field that stores that data from all three objects

2

How is a static method made?



what can it do and not do?

- public/private static variable-type method-name(if setter pass declaration thru here)



- same as regular method except static is before variable type



- useful for conversions that wont change. Can be used or blocked by user like any other method.


- Will not be copied with each instance of the class

How do you use a static method and a static variable or "member" in the main method?
- ClassName.StaticMethodName();
- ClassName.StaticVariableName;

- call the class name directly, make an instance of the class and call the method with the object you created, just call directly from the class

What is an overloaded method?



why would this be convienient (give one example)

a method with the same name but different declaration variables (int vs double)



if you wanted to calculate something two different ways or if you wanted to calculate something but you know you were going to use different variables to do it



What is an overloaded constructor?
- works the same way as a regular constructor and an overloaded method in that the java compiler can tell them apart based on the data types it accepts

- this can be useful if you you you will have objects with multiple variables when they are initialized. It allows you to sent one variable, all the variables or none of the variable if you create an overloaded constructor for each possibility
How do you display the contents of an object without using the methods of that object?

Hint *static methods*

show proper syntax
{
*in main method*{
StuffClass stuff = new Class(1, 3);
displayItem(stuff);
}
*outside of main method but still in main method's class*
public static void displayItem(StuffClass i){
System.out.println(i.getStuff1());
System.out.println(i.getStuff2());
}
}
*outside of main method's class**
Explain the above card
- you initialize the object and pass variables to its constructor, or you can uses setters to assign values
- then you pass the object to a static method inside the main method
- the static method is written just outside the main method brackets but still in the main methods class.
- this method accepts an object from a specified class and creates a new object name only applicable inside that method but still references the object that was initialized in the main method (in this case its "i") and then uses its methods to display its contents
How can you change the contents of an object without calling methods using methods in the form objectName.methodName();?
You can change the objects contents using the syntax two cards up except you change the contents using a setter instead of using a getter to display information
- How can you use a method to return a reference of an instance of a class.
- In other words: how else can you declare an object without declaring it somewhere in the main method?
*Inside main method*
item = getRepurpose();
} *not inside main method*
public static StuffClass getRepurpose(){
int = num1;
int = num2;
system.out.println("how much stuff in 1");
num1 = keyboard.nextInt();
system.out.println("how much stuff in 2"):
num2 = keyboard.nextInt();
return new StuffClass(num1, num2);
}

What is the toString() method used for?

How is it used?
- It returns the current state of the object as a string. so any relavent fields will be used in this method to tell the user the specified contents of an object p 393-395

- you dont even need to call the two string method to use it as shown below

StuffClass stuff = new Stuff(1, 3);
System.out.println(stuff); or
System.out.println("the stuff in this is " + stuff);

- if you wrote a toString() method, java will call it automatically and execute it if you use it in the above fashion
How do you make an equals method?
show proper syntax

Where is this method located?
int sticks = 3;
string butt = "hole";

public boolean equals(StuffClass object2)
if (butt.equals(object2.butt) && sticks = ob ject2.sticks){
return true;
} else{
return false;
}
how do you compare two objects to see if they are equal?
- you must make an equals method, the == operator only compares their memory addresses
- then the statement would look like this
StuffClass stuff1 = new StuffClass(3, "hole");
if/while/etc (Stuff1.equals(stuff2)){
whatever you want in here
}
how can you make two objects that reference eachother (e.i. the same location in memory)
StuffClass stuff1 = new StuffClass(1, fart);
StuffClass stuff2 = stuff1;

-now if you change something in stuff1, it will change it in stuff2 as well, and vice versa
- is a gotcha
How do you make a copy() method in a class
You must make a copy() method in the class that the objects are referencing.
int sticks = 3;
String butt = "hole";

public StuffClass copy(){
StuffClass stuffCopy = new StuffClass(sticks, butt);
return stuffCopy;
}
How can you copy an object to make a duplicate that does not reference the same memory address but has the same field values?
StuffClass stuff1 = new StuffClass(3, "hole");

StuffClass stuff2 = stuff1.copy();

- its important to remember the only info that will be duplicated is from fields that are used in the copy method.
- Also, the copy method can only assign values to a new object if there is (preferably) a constructor to accept them or if there setters in the class to set the values (not preferable)
how can you duplicate an object of the same class with a constructor?
int sticks;
String butt;

public StuffClass (StuffClass stuff2){
sticks = stuff2.sticks;
butt = stuff2.butt;
}

- this can only copy objects of the same class
- you can mutate/copy objects from one class to another class but you'd have to make a constructor for each class possiblity and get more detailed on the declaration
butt = sand.getButt();
how do you duplicate an object in its declaration provided that it has a copy constructor
StuffClass stuff1 = new StuffClass(3, "Hole");
StuffClass Stuff2 = new StuffClass(stuff1);
What is aggregation?
- when you have a class inside of a class
reference pages 402-408 to see how you can use the above methods to build an "umbrella" class using a series of smaller classes with copy constructors and then display the information with toString() methods
when copying aggregate objects (objects that contain multiple other objects/classes) what is the difference between a deep copy and a shallow copy?
- shallow copy = a copy of an aggregate object that where the subclasses are only reference copies, not independent objects themselves

- deep copy = a copy of an aggregate object that creates a new memory address for itself as well as new memory addresses for each of its subclasses but all with the same field values as the object its copying
- if an aggregate object has 3 subclasses, and you perform a deep copy, you will create 4 new memory addresses. 3 for the sub classes and 1 for the aggregate object
When returning an object from a method of an aggegate class, what is the proper syntax? what do you want to avoid?
StuffClass stuff1;
Correct
public StuffClass getStuff(){
StuffClass stuff2 = new StuffClass(stuff1);
return stuff2;
}Inncorrect
public StuffClass getStuff(){
return stuff1;
}
- you want to copy the object and send the user the memory address of the copied object so that they dont have access to the objects original private information
How do you call a constructor from another constructor of the same class?

why would you want to?
int sticks;
String butt;
original
public StuffClass(int stck, String but){
Sticks = stck;
but = butt;
} calling on original
public StuffClass(int stck){
this(stck, "hole");
}
- if you do this you must satisfy the original constructor with the order of the variable and you make make sure it has all the variables its looking for
- this is useful if you only know some of the information, it allows you to enter the known information and assign the rest a default value (in this case "hole")
- Also must be the first statement in the constructor
How do you define your own variable parameters using enumerated types?

use days of the week as an example
enum day {MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY}

- must start with the key word enum
- followed by the variable type name (this would be equivalent as "int" or "double" or "char")
- these are not strings because string is not a variable type, this is why they are not in " "'s
- order matters

how do you assign an enumerated type to a variable?
day workday = day.WEDNESDAY
The values you stored as parameters for your enumerated variable are actually objects.
What methods do those objects have immediatly?
toString = converts each value to a string

ordinal = returns the ordinal value/placement/hierarchy of that value relative to the others, the first value is 0 not 1

compareTo = returns "-" value if the object the compareTo method is operating on is less than the object its comparing to, zero if the same and "+" if its greater (smallObject.compareTo(biggerObject) = "-") only works on objects of the same type

equals = compares two enumerated objects of the same type
What must you remember about declaring enumerated types inside classes
they cannot be declared inside a method
What must you remember about using enumerated types with switch statements?
do not fully qualify them in a case satement
case WEDNESDAY:
good
case day.WEDNESDAY:
bad
what does the finalize method do?
allows you to execute code jest before garbage collection deletes it from memory