More About Objects and Methods
Defining Constructors
~A special method called when instance of an object created with new create objects and initialize values of instance variables.
~ Can have parameters
-To specify initial values if desired.
~ May have multiple definitions
-Each with different numbers or types of parameters.
~ Constructor without parameters is the default constructor.
~If you do define a constructor, Java will not automatically define a default constructor.
Static Variables
~ Also called class variables and contrast with instance variables.
~Do not confuse class variables with variables of a class type.
~Both static variables and instance variables are sometimes called fields or data members.
~Static variables are shared by all objects of a class.
~Variables declared static final are considered constants value cannot be changed but variables declared static without final can be changed.
~Only one instance of the variable exists and it also can be accessed by all instances of the class.
Static Methods
~ Some methods may have no relation to any type of object.
~Example :
-Compute max of two integers
-Convert character from upper- to lower case
~Static method declared in a class and can be invoked without using an object.
Wrapper Classes
~Recall that arguments of primitive type treated differently from those of a class type and may need to treat primitive value as an object.
~Java provides wrapper classes for each primitive type.
~Methods provided to act on values.
~Allow programmer to have an object that corresponds to value of primitive type.
~Contain useful predefined constants and methods.
~Wrapper classes have no default constructor so programmer must specify an initializing value when creating new object.
~Wrapper classes have no set methods.
Decomposition
~Recall pseudocode.
-With this pseudocode we decompose the task into subtasks
-Then solve each subtask
-Combine code of subtasks
-Place in a method
Addressing Compiler Concerns
~ Compiler ensures necessary tasks are done
-initialize variables
-return statement
~Rule of thumb: believe the compiler
-change the code as requested by compiler
-most likely correct
Overloading Basics
~ When two or more methods have same name within the same class.
~Java distinguishes the methods by number and types of parameters.
~If it cannot match a call with a definition, it attempts to do type conversions.
~A method's name and number and type of parameters is called the signature.
Packages
~ A package is a collection of classes grouped together into a folder.
~Name of folder is name of package.
~Classes use packages by use of import statement.
~Package name tells compiler path name for directory containing classes of package.
~Search for package begins in class path base directory.
~Package name uses dots in place of / or \ .
~Name of package uses relative path name starting from any directory in class path.
Name Clashes
~ Packages help in dealing with name clashes when two classes have same name.
~Different programmers may give same name to two classes for ambiguity resolved by using the package name.
Tiada ulasan:
Catat Ulasan