



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 6
This page cannot be seen from the preview
Don't miss anything!




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:
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
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 ) );