Ex10CrazyNaming

Ex10CrazyNaming - // notice the + sign } // end of method...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
Example 10: Variable Scope (which variable is it?) /* class CrazyVariables * Author: Dr. Franklin * Purpose: Illustrate the scope rules of Java - which variables are * accessed from where */ import java.util.Scanner; public class CrazyVariables { public static void main(String args[]) { Scanner input = new Scanner( System.in ); int num; // local variable System.out.print("Enter a number: "); num = input.nextInt(); System.out.print("\n"); CrazyClass crazy = new CrazyClass(num-5); num = crazy.doSomething(num+3); num = crazy.doSomethingElse(num+5); System.out.println(“Answer is: “+num);
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: // notice the + sign } // end of method main } // end of class CrazyVariables /* class CrazyClass * Author: Dr. Franklin * Purpose: Illustrate the scope rules of Java - which variables are * accessed from where */ public class CrazyClass { private int num; // instance variable public CrazyClass(int n) { num = n; } public int doSomething(int num) // num is input argument - local { return (num * 3) // which num is it talking about? } public int doSomethingElse(int n) { return (num – 10); // which num is it talking about? } } // end of CrazyClass...
View Full Document

This note was uploaded on 12/27/2011 for the course CMPSC 5 taught by Professor Franklin during the Fall '09 term at UCSB.

Ask a homework question - tutors are online