SE-1010 Dr. Mark L. Hornick 1 Defining Your Own Classes Part 3.
-
Upload
brian-horn -
Category
Documents
-
view
215 -
download
1
Transcript of SE-1010 Dr. Mark L. Hornick 1 Defining Your Own Classes Part 3.
SE-1010Dr. Mark L. Hornick
1
Defining Your Own Classes
Part 3
SE-1010Dr. Mark L. Hornick
2
A local variable is a variable that is declared within a method declaration
Local variables are accessible only from the method in which they are declared.In this example, output and message are local variables accessible only
from within printSomeMessage
public void printSomeMessage( String message ) {
// textMessage is a local String variable
String output = “The message is “ + message ;
System.out.println(output );
}
SE-1010Dr. Mark L. Hornick
3
In this example, output is a local variablepublic class Student {private String firstName;…
public String toString(){ String output; output = “My name is ” + firstName; return output; }
}
Local variables are only visible to the method in which they are defined.
SE-1010Dr. Mark L. Hornick
4
In this example, x, y, and z are all local variables
public int add( int x, int y ){ int z = x + y; return z;
}
Local variables are only visible to the method in which they are defined.
SE-1010Dr. Mark L. Hornick
5
Rules for Local Variables
A local variable is a variable that is declared within a method declaration. The parameters of a method are also considered local variables
to the method.
Local variables are accessible only from the method in which they are declared.
Memory space for local variables is allocated only during the execution of the method. When the method execution completes, memory space will be
cleared.
SE-1010Dr. Mark L. Hornick
6
A local variable with the same name as a class attribute will hide the attribute within the scope of the local variable
public class Student {private String someString; // attribute…
public void method1(){ String someString; // local var hides attribute someString = “hello”; // uses local variable
}public void method2(){
someString = “se1010”; // uses attribute}
Local variables are only visible to the method in which they are defined.
SE-1010Dr. Mark L. Hornick
7
The this keyword is used to distinguish an object’s attribute value from a local variable in the case of ambiguity
public class Student {private String someString; // attribute…
public void method1(){ String someString; // local var hides attribute someString = “hello”; // uses local variable
this.someString = “there” // uses attribute for “this” object
}public void method2(){
someString = “se1010”; // uses attribute}
The best approach, however, is to avoid local variables that have the samename as attributes.
SE-1010Dr. Mark L. Hornick
8
A local variable is a variable that is declared within a method declaration
Local variables are accessible only from the method in which they are declared.In this example, output and message are local variables accessible only
from within printSomeMessage
public void printSomeMessage( String message ) {
// textMessage is a local String variable
String output = “The message is “ + message ;
System.out.println(output );
}
SE-1010Dr. Mark L. Hornick
9
Passing primitive arguments to methods
When a method containing primitive arguments is called, the value of the actual argument is passed to the matching formal argument, and separate memory space (the Stack) is allocated to store this value.
This way of passing the value of arguments is called a pass-by-value, or call-by-value, scheme.
SE-1010Dr. Mark L. Hornick
10
Example of pass-by-value/call-by-value argument passing from caller to called method
// this method is part of the main classpublic static void main(String args[]) { Calculator calc1 = new Calculator(); int sum = calc1.add( 3, 4);}
. . . .
// This method is part of the Calculator classpublic int add( int x, int y) { return x+y; // return the sum}
The values 3 and 4 are passed as the actual arguments
The formal arguments x and y receive copies of the values provided by the caller
The value resulting from the addition of x+y is passed back to the caller, where it is copied into the variable sum
SE-1010Dr. Mark L. Hornick
11
The datatype of an argument must be assignment-compatible with the data type of the matching formal parameter
When it can, the compiler will provide automatic upcasts of the argument to the datatype of the matching parameter
For example, if you supply an int argument where a long parameter is expected, the compiler will upcast the int to a long
But the compiler will complain if you try to pass a long argument into an int parameter This is called a downcast, and will not be supplied by the
compiler
SE-1010Dr. Mark L. Hornick
12
Passing and Returning Objects
Passing and returning objects follows the same process as passing and returning primitive data types.
The only difference is that with objects, the value being passed is the reference (or address) to an object. When a variable is an object name, the reference of
the variable is the address in memory where the object is stored.
SE-1010Dr. Mark L. Hornick
13
Review: Primitive variables vs. Object variables
The only difference between a variable for a primitive and a variable for objects is the contents in the memory locations. For primitives, a variable contains the numerical value itself.
For objects, a variable contains an address where the object is stored.
10
name <Memory address of String object>
xStringobject
SE-1010Dr. Mark L. Hornick
14
Example of pass-by-reference, or call-by-reference
// this method is part of the main classpublic static void main(String args[]) { String first = “Joe”; student.setFirstName( first );}. . .
// This method is part of the Student classpublic void setFirstName( String name) { firstname = name;}
The identifier first references a String object and is passed as an actual argument
The formal argument name receives the reference to the String object
SE-1010Dr. Mark L. Hornick
15
Another example of pass-by-reference, or call-by-reference
// this method is part of the main classpublic static void main(String args[]) { String s = student.getFirstName( );}. . .
// This method is part of the Student classpublic String getFirstName( ) { return firstName;}
The identifier s receives a reference to a String object
The method returns a copy of the reference to (the address of) the String object containing the name
SE-1010Dr. Mark L. Hornick
16
Dot notation
When you call a public method of an object, you must use dot notation to specify both the method and the object you are calling it on
Dot notation is optional when you call a method from another method if the two methods belong to the same object.
If dot notation is used, use the reserved word this to refer to the same object.
SE-1010Dr. Mark L. Hornick
17
Calling a method belonging to the same object vs. calling a method belonging to a different object.