Terms and Rules Professor Evan Korth New York University (All rights reserved) Class • The main building blocks of Java programs. • Defines objects of.

Download Report

Transcript Terms and Rules Professor Evan Korth New York University (All rights reserved) Class • The main building blocks of Java programs. • Defines objects of.

Terms and Rules
Professor Evan Korth
New York University
(All rights reserved)
Class
• The main building blocks of Java programs.
• Defines objects of the same type. Like a
blueprint.
• Every .java file has one or more classes. Exactly
one of the classes must be a public class.
– That class must have the same name as the .java file.
• If the class has an method called main(), execution
can begin in that class. (Therefore, you can test a
class by adding a main method to it.)
• If there are other classes in the file, they cannot be
public classes.
Class modifiers
• No modifier (default) means the class is
visible in the package in which it is
declared.
• Public means it is visible to everything.
• There are two others (final and abstract)
which we will discuss later in the semester.
Encapsulation
• Encapsulation refers to the the process of
combining elements to create a new entity.
• You encapsulate the properties (attributes)
and behaviors (activities) of an entity into a
class.
• Encapsulation also enables us to hide the
implementation of a class to other classes
(information hiding).
Objects
• An object is an instance of a class.
• If we think of a class as a blueprint, an object is
one model created from that blueprint.
• You can create any number of objects from one
class.
• An object is distinctly identified by an object
reference (except for anonymous objects).
Declaring object references
• In order to reference an object, we need an object
reference variable.
• To declare an object reference variable we use the
syntax:
ClassName objectReferenceName;
• The above statement creates a variable
objectReferenceName which can reference a
Classname object. It does NOT create an object.
Instantiating objects
• In order to create an object, we use the new
keyword along with a constructor for the class of
the object we wish to create.
• To refer to the object, we “point” an object
reference variable to the new object.
objectReferenceName = new Constructor();
• The decalration and instantiation can be combined
as follows:
ClassName objectReferenceName = new Constructor();
* More on constructors soon
Anonymous objects
• An object without a reference is called an
anonymous object.
• It is created, used and immediately marked
as garbage.
members
• A class declaration includes members of the class.
• A member can be either a data member or a
method member.
• A data member (AKA field) is used to define state
(attributes or properties) of the entity.
• A method member is used to define the behaviors
of the entity.
Data members
• Data members can be a primitive type or a
reference to another object.
– Primitive types are integer types, float types and char,
Boolean. (Note: an int is not the same as an object of
type Integer)
• A data member can be either an instance variable
or a static variable (also known as a class
variable).
• The scope of a data member is the entire class it is
declared in no matter where within the class it is
declared.
Default values for data members
• 0 for all numeric type variables (including
float types and integer types)
• \u0000 for char variables
• null for reference variables
• False for Boolean type variables
• Note: No default values for method
variables.
Static variable (AKA class variable)
• A static variable has only one value no matter how many
objects are instantiated from a class. The value is shared
by all instances of the class.
• A static variable does not need an instance of the class in
order to be accessed.
• You can access a static variable either with
ClassName.varName (better style), or
objectReference.varName notation.
• For static variables, every object of the class refers to the
same memory location.
• Static variables can be accessed by static methods OR
instance methods.
• The memory for a static variable is allocated when the
class is loaded into memory.
Instance variables
• An instance variable has a unique value for each
object of that class.
– This does not mean that two objects cannot have the
same value; it does mean that those values will be
stored separately in memory.
• You can access an instance variable only with
objectReference.varName notation.
• No memory is allocated until an object is
instantiated.
• Can be accessed by instance methods only.
Data modifiers
• No modifier (default) means the data is visible in the
package in which it is declared.
• Public means the data is visible to everything.
• Private means the data is visible only within the class
in which it is defined.
– Trying to access private data from another class will result in
a compile time error.
• Static means it is a static variable. Static variables can
use other modifiers as well.
• Final means the variable cannot be changed.
• There are two others (protected and abstract) which we
will discuss later in the semester.
• There are still others which we will not discuss this
semester.
Method members
• Methods are used to define the behaviors of
an object.
• They can be overloaded.
– Having more than one method in a class with
the same name is referred to method
overloading.
– Each of the methods must have a different
method signature. That is, they must have
different argument lists.
Local method variables
• Do not automatically get initialized.
– Using them without initializing them is a
compilation error.
• Cannot have visibility modifiers.
• The scope of a local method variable starts
where it is declared. It ends at the end of
the block where it was declared.
Method modifiers
• No modifier means the method is visible in the package
in which it is declared.
• Public means the method is visible to everything.
• Private means the method is visible only within the
class in which it is defined.
– Trying to call a private method from another class will result
in a compile time error.
• Static means it is a static method. Static methods can
use other modifiers as well.
• There are three others (final, protected and abstract)
which we will discuss later in the semester.
• There are still others which we will not discuss this
semester.
Static methods (AKA class methods)
• Can be called without an instance of the method.
• All the methods in the Math class are static
methods which is why we can call them without a
Math object. In fact we cannot instantiate an
object of the Math class.
• You can call a static method either with
ClassName.method (args) (better style), or
objectReference.method (args) notation.
Instance methods
• Can only be called after an object is
instantiated.
• You can call an instance method only with
the objectReference.method (args) notation.
• An instance method acts on the specific
instance for which it has been called.
Constructors
• Constructor are special methods that instantiate
objects.
• A constructor is invoked with the new operator.
• A constructor:
– should initialize the instance variables;
– can also modify static variables;
– can also do anything else but is usually used just for the
above.
• A constructor does not have a return type.
• A constructor’s identifier (name) is the same as the
class it constructs.
Constructors continued
• Constructors can be overloaded but each one must
have its own signature.
• A constructor with fewer arguments can call a
constructor with more arguments.
• A constructor with no arguments is called a default
constructor.
– If no constructor is defined, a default constructor is
automatically supplied. Variables are initialized to their
default values.
– If one constructor is explicitly defined, the automatic
default constructor is no longer available. In such case,
if you want a default constructor, you must define it
yourself.
Accessor methods
• When a data member is declared to be
private, we still need a way to refer to that
data. A method used to change or retrieve a
private data item is referred to as an
accessor method.
• Two kinds of accessor methods are the get
method and the set method.
Get methods
• A method that is used to retrieve the value of a
data object is referred to as a get method.
• Also known as a getter.
• Get method header should look like this:
public returnType getPropertyName ()
• It may just return a data field or it may calculate
the value. Remember information hiding.
Predicate methods
• A get method that returns a Boolean value
should have a header like this:
public boolean IsProperty ()
• It can simply return a Boolean data field or
it can use a Boolean formula to calculate it’s
data. Remember, information hiding!
Set methods
• Methods used to set or change the value of a
data method are referred to as set methods.
• Also known as setters and mutators.
• Header of set method will look like this:
public void setProp (propType)
Garbage collection
• When an object is no longer referenced by any
reference variable, that object is referred to as garbage.
• Java automatically tracks garbage objects and returns
the memory to the operating system when the garbage
collector runs.
• We do not have direct control over when the garbage is
collected.
• We can suggest to the compiler to collect garbage but it
is not guaranteed that it will run.
• To suggest garbage collection we make the following
method call:
– System.gc();
Keyword this
• The keyword this is used with a class to refer to
the specific instance of the class that is being used.
• A variable in a class’ method that has the same
name as a field will “shadow” the field. You can
access the field using the this keyword.
• This (args) in a constructor will invoke another
constructor of that class.
– If you call another constructor from a constructor, it
must be the first line in the calling constructor.
• You cannot use the this keyword in static methods.
(why?)
Passing variables to methods
• All variables in Java are passed using call by
value. However, since object variables are really
references to objects, passing an object is
simulated pass by reference.
– Objects passed to a function and modified by that
function will have the changes reflected in the calling
function.
– Primitive variables passed to a function and modified
by that function will NOT have the changes reflected in
the calling function.
Principle of least privilege
• You should pick the modifier that allows the
least privilege for other classes while
allowing your code to do what it needs to
do.
• This helps reduce debugging time by
localizing potential problem areas.
Exam
• Our only midterm will be given the week of
October 20th
– Either the 21st or the 23rd