DOC PREVIEW
Penn CIT 591 - Static Methods

This preview shows page 1-2-3 out of 8 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Static MethodsAbout methodsWhy methods?Defining a static methodReturning a result from a methodReturning no result from a methodNon-static methodsThe EndJan 14, 2019Static MethodsAbout methodsA method is a named group of declarations and statementsYou execute those declarations and statements by calling the methodWhen you call the method, you can give it parameters (information, such as numbers, going into the method)A method typically has a return value (a single piece of information coming out of the method)Declarationsand statementsparameterparameterparameterreturn valueWhy methods?Methods help you break up a complex problem into simpler subproblems, which you can solve separatelyMethods can use other methods, but should not depend on the “inner workings” of those other methods--just what they do, not how they do itMethods allow you to give names to parts of your computation, thus making your program more readableReadability is essential in good programmingProper choice of method names is importantVerbs are usually best, since methods “do something”If you have anything the least bit complicated to compute, you should write (and debug) it in only one placeThis is an application of the DRY principle (“Don’t Repeat Yourself)Methods are very good for putting code in only one placeDefining a static methodA static method has the syntax: static return-type method-name ( parameters ) { method-variables code}Example: static boolean isAdult(int age) { int magicAge = 21; return age >= magicAge;}Example: static double average(int a, int b) { return (a + b) / 2.0;}Returning a result from a methodIf a method is to return a result, it must specify the type of the result:static boolean isAdult ( …You must use a return statement to exit the method with a result of the correct type:return age >= magicAge;Returning no result from a methodThe keyword void is used to indicate that a method doesn’t return a valueThe return statement must not specify a valueExample:static void printAge(String name, int age) { System.out.println(name + " is " + age + " years old."); return;}There are two ways to return from a void method:Execute a return statementReach the closing brace of the methodNon-static methodsThis slide set only talks about static methodsMost methods in a Java program will not be staticHowever, before we can talk about what it means to be static, we have to introduce classes and methodsSo, more to come...The


View Full Document

Penn CIT 591 - Static Methods

Documents in this Course
Stacks

Stacks

11 pages

Arrays

Arrays

30 pages

Arrays

Arrays

29 pages

Applets

Applets

24 pages

Style

Style

33 pages

JUnit

JUnit

23 pages

Java

Java

32 pages

Access

Access

18 pages

Methods

Methods

29 pages

Arrays

Arrays

32 pages

Methods

Methods

9 pages

Methods

Methods

29 pages

Vectors

Vectors

14 pages

Eclipse

Eclipse

23 pages

Vectors

Vectors

14 pages

Recursion

Recursion

24 pages

Animation

Animation

18 pages

Animation

Animation

18 pages

Static

Static

12 pages

Eclipse

Eclipse

23 pages

JAVA

JAVA

24 pages

Arrays

Arrays

29 pages

Animation

Animation

18 pages

Numbers

Numbers

21 pages

JUnit

JUnit

23 pages

Access

Access

18 pages

Applets

Applets

24 pages

Methods

Methods

30 pages

Buttons

Buttons

20 pages

Java

Java

31 pages

Style

Style

28 pages

Style

Style

28 pages

Load more
Download Static Methods
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Static Methods and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Static Methods 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?