{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Intro to Java Web-Notes_Part19

Intro to Java Web-Notes_Part19 - return 753 The give Val...

Info icon This preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: return 753; The give Val method above returns the int value 753. The call to the method, give ValO, returns, or gives out, the value 753. This int value can be used like any other integer -- in the code above the 753 is simply assigned to the integer variable w. It is more interesting if the method calculates a value to return, versus just returning the exact same value everytime the method is called: public class ShowMethods9 { public static void main(String args) { Demo dm = new Demo(); dm.first(); } class Demo { void first() { double num; num : giveSquare(5.9); System.out.println("Value is: " + num); } double giveSquare(double inVal) { double squaredVal; squaredVal = inVal * inVal; return squaredVal; The program above generates this output: Value is: 34.81 Of course, a value returning method can be called multiple times: public class ShowMethodle { public static void main(String args) { Demo dm : new Demo(); dm.first(); } class Demo { void first() { double num; num : giveSquare(5.9); System.out.println("Value is: " + num); num = giveSquare(l6l.35); System.out.println("Value is: " + num); } double giveSquare(double inVal) { double squaredVal; squaredVal = inVal * inVal; return squaredVal; } The above program gives the following output: Value is: 34.81 Va;ue is: 26033.8225 If a method is not going to return any value, its type is declared as void. Methods -- Scope The scope of a variable The scope of a variable is the set of instructions that can see that variable. A variable's scope is limited to the nearest set of enclosing curly braces: {}. The following examples look at the scope of an integer variable x. The scope of x is indicated by the code that is printed in bold font. 1. In this example the variable X is "local to", or contained within, the method book, so X's scope is limited to the instructions inside the book method. Since the scope of X is limited to the book method, it cannot be seen or used by instructions found anywhere else in the program (the instructions in first cannot refer to X, and the instructions in main cannot refer to X). public class ShowMethods { public static void main(String args) { Demo dm = new Demo(); dm.first(); } class Demo { void first( ) { statementl; statementZ; book(); statement3; } void book() { int x = 75; // x is enclosed by the book method's {}s statement4; statement5; statement6; } 2. With regard to scope, a method’s parameters are considered to be local to (enclosed within) the body of the method. Therefore, the scope of the parameter x, below, is limited to the book method: public class ShowMethods { public static void main(String args) { ...
View Full Document

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern