using a package member from outside its package
must use fully qualified name to use class (member) |
animals.Dog myDog = new animals.Dog(); |
this is from the animals package |
how to prevent
create a deep copy in the constructor
condition for class to be immutable
if all fields are private and no mutator methods |
only wao to assign values to fields through constructor |
if public or set type is provided, its mutable |
if fields are private and no get or set methods...its immutable |
static vs non-static
static is called on class |
from outside class: ClassName.methodName() |
nonstatic is called on specific object |
call from outside class: obj.methodName() |
reference static from outside class |
ClassName.varName |
reference obj from outside class |
obj.varName |
changing static
g2.msg, g.msg |
change g2.msg if static will change g.msg |
|
|
java auto imports two packages for each src file
java.lang package |
the current package |
try this!
in patient class, overload the constructor by adding another one that takes as input age, name, temperatures of new patient |
does it make a difference if we copy the elements of the input array, or just the address directly |
this field in static method
if field is package private, (no access modifier)
can access as long as classes are in the same package |
public class Dog { String name';} |
|
from class beagle, can access through d.name |
|
|
overloading constructor
create two constructors in same class file |
one takes no input, other takes input |
does a constructor have to initialize all fields?
no...other fields will be initialized by default |
in java you cannot assign default value in args |
static method access
if method is static, has access to class variables |
if method is not static, has access to both class and instance variables |
can only access field by class name if
public and static, not public is insufficient |
the result of the new operator is
a reference to the new object |
Random randomGenerator = new Random(); |
|
|
methods; same package but different classes
we need to use name of class.method to access |
can we access private field from constructor?
january 19 second slide |
no cannot, need getter or setter...but the field is initialized just not accesible |
in what case do we try to leave fields public?
private vs public fields
private field is accessible only within class in which it was written |
public field is accessible from anywhere |
package private is only accessible within class, or its own package...not accessible to world |
|