CSIS 110 Lecture 29: Class Variables, Name Reuse, Method Calls, Style and Overloading - Pr, Study notes of Javascript programming

A lecture note from csis 110 course, covering topics such as class variables, name reuse and hiding, method calls, method style and overloading. The concept of class variables, their creation and usage, and the difference between class and instance variables. It also discusses the concept of name reuse and hiding, and the rules for calling one method from another. Furthermore, it introduces the concept of method style and method overloading, and provides examples and good practices for writing methods. The document also includes a quiz scheduled for march 28.

Typology: Study notes

Pre 2010

Uploaded on 08/05/2009

koofers-user-s6c
koofers-user-s6c ๐Ÿ‡บ๐Ÿ‡ธ

10 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSIS 110 โ€“ Lecture 29
Reading โ€“ Sections 7.5 and 7.6
Quiz โ€“ Monday, March 28
Last time we discussed class variables, name reuse, and name hiding
Class variables
A class variable is independent of the objects in the class. There is only one instance of a
class variable, regardless of how many objects exist.
Let's look at a class that includes a class variable - Counter.java
The methods reset(), getValue(), and click() should all be pretty familiar to you -
they are instance methods.
Method getHowMany() is a class method - note the keyword static
The variable howMany is a class variable - again, note the keyword static.
howMany gets initialized to 0, and is incremented everytime the Counter constructor is
invoked.
[Draw picture]
Class variable gets created only once โ€“ when you first reference the class.
Instance variables get created for every object โ€“ every object has its own set of instance
variables.
Name Reuse and Hiding
Last time we looked at reusing variable names, and we saw that there were certain
situations where we could declare a variable that hides or shadows another variable of the
same name. In particular, a local variable or a FP can hide an instance or class variable.
Let's look at some examples - see Scope.java
Can I make these definitions:
-define a for loop control variable x in main after local variable x
-define a local variable x in print()
-define an instance variable x in Scope
Can I define a local variable x in print2()?
pf3
pf4
pf5

Partial preview of the text

Download CSIS 110 Lecture 29: Class Variables, Name Reuse, Method Calls, Style and Overloading - Pr and more Study notes Javascript programming in PDF only on Docsity!

CSIS 110 โ€“ Lecture 29

Reading โ€“ Sections 7.5 and 7. Quiz โ€“ Monday, March 28 Last time we discussed class variables, name reuse, and name hiding Class variables A class variable is independent of the objects in the class. There is only one instance of a class variable, regardless of how many objects exist. Let's look at a class that includes a class variable - Counter.java The methods reset(), getValue(), and click() should all be pretty familiar to you - they are instance methods. Method getHowMany() is a class method - note the keyword static The variable howMany is a class variable - again, note the keyword static. howMany gets initialized to 0, and is incremented everytime the Counter constructor is invoked. [Draw picture] Class variable gets created only once โ€“ when you first reference the class. Instance variables get created for every object โ€“ every object has its own set of instance variables. Name Reuse and Hiding Last time we looked at reusing variable names, and we saw that there were certain situations where we could declare a variable that hides or shadows another variable of the same name. In particular, a local variable or a FP can hide an instance or class variable. Let's look at some examples - see Scope.java Can I make these definitions:

  • define a for loop control variable x in main after local variable x
  • define a local variable x in print()
  • define an instance variable x in Scope Can I define a local variable x in print2()?

Looking back at scope, we see that the local x in main() hides the class variable x, and that the formal parameter x in print() also hides the class variable x. This is called hiding or shadowing. What if we want to access the class variable? I can use its class name to make it clear that I want to use the class variable. Now lets look at Scope2.java - this uses instance variables instead of class variables. I declare an instance s of type Scope2, and then call its instance methods. What if I want to access the instance variable x? I can't use the class name - its not a class variable. Normally, I would use an object name, but inside of the instance methods I don't have a name - I just have the implicit object that I am using - my current object. Instead, I use this. Calling one method from another method in the same class The statements in the body of a method can include method invocations. Let's look at Triangle class - we can see that some of the methods call others. What are the rules for calling one method from another? Within any particular class: An instance method can call another instance method on the same object - don't need to use an object name, as we already have the this object. (can invoke using this.method() ). An instance method can call a class method in the same class - no class name is required. A class method can call another class method in the same class - no class name is required. A class method cannot call an instance method - why not? Class X { private int value; public static void method1( int a ) { method2( a ); // Not valid } public void method2( int a ) { value = a; }

Now, I can use this method with 2 doubles: double first = stdin.nextDouble(); double second = stdin.nextDouble(); double larger = max( first, second ); I can also do this: int first = stdin.nextInt(); int second = stdin.nextInt(); double larger = max( first, second ); This is OK, but not really that great. I could change the last statement to be: int larger = (int) max( first, second ); This is still not so great - does a lot of unecessary work converting ints to doubles and back again. I'd really like this: int first = stdin.nextInt(); int second = stdin.nextInt(); int larger = max( first, second ); Java allows me to do this by defining a second method with the same name! public int max( int a, int b ) { if ( a > b ) return a; else return b; } This is called method overloading. Method overloading is useful when we want to have methods that do similar tasks, but need to operate on

  • different types of parameters
  • different numbers of parameters Java allows a method name to be overloaded as long as the signature of the methods differs. A method's signature is
  • the method's name
  • the types of the parameters
  • the order of the parameters
  • the number of parameters Note that the signature does NOT include
  • the parameter names
  • the return type

The book is WRONG - in section 7.6 on page 344 it says that the method signature includes the names of the parameters - this is NOT TRUE. Let's look at some examples - what is the signature of public boolean isIsoceles(); public static max( int a, int b ); public static double sqrt( double x ); public static int sqrt( int x ); public We've actually seen method overloading already without realizing it: System.out.println() String.valueOf() Without overloading, we would have to have separate methods for each of these - printString, printInt, printDouble, printChar, etc. How does Java decide which method to use when an overloaded method is called? First, it sees if there is an exact type match of actual and formal parameters. Then, it widens actual parameters as necessary to match. public static void main( String[] args ) { System.out.println( "the larger of 3 and 5 is " + max( 3, 5 ) ); System.out.println( "the larger of 3.2 and 4.7 is " + max( 3.2, 4.7) ); System.out.println( "the larger of 3 and 4.7 is " + max( 3, 4.7 ) );
} Let's look at Ambiguous.java public static double f( int x, double y ) { return x / y; } public static double f( double x, int y ) { return x * y; } public static void main( String[] args ) { System.out.println( f( 3, 1.5 ) ); System.out.println( f( 1.25, 4 ) ); System.out.println( f( 2, 3 ) );