Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Coding 4 (part2) David Davenport Computer Eng. Dept.,

Similar presentations


Presentation on theme: "Java Coding 4 (part2) David Davenport Computer Eng. Dept.,"— Presentation transcript:

1 Java Coding 4 (part2) David Davenport Computer Eng. Dept.,
Be aware… BEWARE! David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Last updated: 4/11/2017 ~ separated out last part with global variables! Previously: from original 16/11/2016 NOTICE: This presentation deals almost exclusively with STATIC methods & structured programming. We move on to INSTANCE methods in a following presentation on OOP.

2 IMPORTANT… Students… Instructors…
This presentation is designed to be used in class as part of a guided discovery sequence. It is not self-explanatory! Please use it only for revision purposes after having taken the class. Simply flicking through the slides will teach you nothing. You must be actively thinking, doing and questioning to learn! Instructors… You are free to use this presentation in your classes and to make any modifications to it that you wish. All I ask is an saying where and when it is/was used. I would also appreciate any suggestions you may have for improving it. thank you, David.

3 Be aware Want methods to be reusable
Methods that read inputs from keyboard or write output to console are difficult to reuse (e.g. in GUIs) So, pass inputs as parameters & return the output as the result User interaction is then done elsewhere, usually main method. E.g. if a sqrt method allows read its input from the keyboard, we could not use it to compute hypothenuse. Or if it prints output on console, we could not use it to output sqrt * 2 + 1, or some such. Ok, you can define methods specifically for getting data from user/keyboard, or for outputting data to the console in a neat form, but that is all they should do. Principle: each piece performs only one function… Cohesion & coupling… Method should do one task only & should rely on as few external methods/classes as possible (& should definitely not rely on their internal details!)

4 Beware a = 5; b = 3; d = 7; c = myMethod( a, b);
Sys… ( a + “, ” + b + “, ” + d); What is output? In Java, (primitive) parameters are inputs only ~ “pass by value”. In other languages, parameters can be outputs & input/outputs too (indeed, Java object parameters behave differently! ~ “pass by reference”) d is normally assumed not to be changed by the method… (if defined locally in method then (hopefully) no problem but if “global” –static class variable- then dangerous, all bets are off. Only way to be sure in such cases is to trace method.. But tracing is error prone, especially if 10,000 lines long!) Note: global constants are ok, indeed encouraged! Of course, assumes one more thing… only one thread! Naturally assume method parameters are inputs only & method doesn’t have side effects! Global variables!

5 Global, Local, Params… public class MethodPlay { static int d; } global Scope: if local use it, else look to outer (global) context. Rule: any variable used in a method, that is not a parameter, must be defined locally… otherwise need to trace everything to see whether any side-effects change its value! public static int myMethod(…) { d = 999; } assigns 999 to global variable public static void main( String[] args) { d = 7; c = myMethod(…); Sys..( d); } assigns 7 to global variable prints 999 from global variable

6 Global, Local, Params… public class MethodPlay { static int d; } global Scope: if local use it, else look to outer (global) context. Rule: any variable used in a method, that is not a parameter, must be defined locally… otherwise need to trace everything to see whether any side-effects change its value! public static int myMethod(…) { d = 999; } assigns 999 to global variable public static void main( String[] args) { int d; d = 7; c = myMethod(…); Sys..( d); } local Note: - need global d here otherwise won’t compile (missing d in myMethod) - can get value (=999) from global variable using “MethodPlay.d” assigns 7 to local variable prints 7 from local variable

7 Global, Local, Params… public class MethodPlay { static int d; } global Scope: if local use it, else look to outer (global) context. Rule: any variable used in a method, that is not a parameter, must be defined locally… otherwise need to trace everything to see whether any side-effects change its value! public static int myMethod(…) { int d: d = 999; } local assigns 999 to local variable public static void main( String[] args) { d = 7; c = myMethod(…); Sys..( d); } Note: - need global d here otherwise won’t compile (missing d in myMethod) - can get value (=999) from global variable using “MethodPlay.d” assigns 7 to global variable prints 7 from global variable …unless something else changed it!

8 assigned to param (input only!)
Global, Local, Params… public class MethodPlay { static int d; } global Scope: if local use it, else look to outer (global) context. Rule: any variable used in a method, that is not a parameter, must be defined locally… otherwise need to trace everything to see whether any side-effects change its value! public static int myMethod( int d) { d = 999; } param assigned to param (input only!) public static void main( String[] args) { int d; d = 7; c = myMethod(..); Sys..( d); } local

9 Global, Local, Params… public class MethodPlay { static int d; } global Scope: if local use it, else look to outer (global) context. Rule: any variable used in a method, that is not a parameter, must be defined locally… otherwise need to trace everything to see whether any side-effects change its value! public static int myMethod( …) { int d; d = 999; } local public static void main( String[] args) { int d; d = 7; c = myMethod(..); Sys..( d); } local

10 Example… static Scanner scan = new Scanner( System.in );
global public static int askForAndGetIntValue() { System.out.print( “Enter value: “); return scan.nextInt(); } Scanner scan ) { param Scanner scan = new Scanner( System.in ); local oops… undefined! Local: ok, but may be problem here due to shared resource? Param: ok, create in main & pass (but may have lots of params!) Global: um… for the really lazy!


Download ppt "Java Coding 4 (part2) David Davenport Computer Eng. Dept.,"

Similar presentations


Ads by Google