Writing Static Methods Up until now, we have been USING (calling) static methods that other people...

11
Writing Static Methods • Up until now, we have been USING (calling) static methods that other people have written. • Now, we will start CREATING our own static methods, so that we can call them later on in other programs we write

Transcript of Writing Static Methods Up until now, we have been USING (calling) static methods that other people...

Page 1: Writing Static Methods Up until now, we have been USING (calling) static methods that other people have written. Now, we will start CREATING our own static.

Writing Static Methods

• Up until now, we have been USING (calling) static methods that other people have written.

• Now, we will start CREATING our own static methods, so that we can call them later on in other programs we write

Page 2: Writing Static Methods Up until now, we have been USING (calling) static methods that other people have written. Now, we will start CREATING our own static.

Creating a Method

• A method has the following structure:modifer returnValueType methodName(list of input parameters) {

method variables declarationmethod body

return value}

• The part in purple above is called the method signature...a way of identifying the method and quickly seeing what it needs to work and what it returns.

Page 3: Writing Static Methods Up until now, we have been USING (calling) static methods that other people have written. Now, we will start CREATING our own static.

modifer returnValueType methodName(list of input parameters) {

method variables declarationmethod body

return value}

• The modifier section contain words that describe the method, just as public and static. – public means that anyone can call or use the method.

There are other things you can say besides public, but we won't use them for now.

– static means that the method is a "class method", unchanging and depending only on the input parameters to do what it is supposed to do. If a method has the word static in its signature, you call a it by saying: ClassName.methodName(inputValues);

Page 4: Writing Static Methods Up until now, we have been USING (calling) static methods that other people have written. Now, we will start CREATING our own static.

modifer returnValueType methodName(list of input parameters) {

method variables declarationmethod body

return value}

• The returnValueType section says what the method returns when it is called. A method can only return one value, like an int or a double. If the method doesn’t return anything, its returnValueType is void.– If a method has int as the return type, its result can be assigned

to an int variable or printed out using System.out.println()– If a method has void as the return type, you CANNOT put the

method inside of a System.out.println(), because it doesn’t return anything to print!

Page 5: Writing Static Methods Up until now, we have been USING (calling) static methods that other people have written. Now, we will start CREATING our own static.

modifer returnValueType methodName(list of input parameters) {

method variables declarationmethod body

return value}

• The methodName is what the function is called. • The rules for method names are the same as

the rules for variable names and class names. – Method names can only be one word– Usually, the first letter of method names is written

in lowercase

Page 6: Writing Static Methods Up until now, we have been USING (calling) static methods that other people have written. Now, we will start CREATING our own static.

modifer returnValueType methodName(list of input parameters) {

method variables declarationmethod body

return value}

• The list of input parameters is a list of variable declarations that say what the method needs from the person calling it in order to work.

• Just like you do with any variable declaration, when writing the list of input parameters you need to list both the type and name of each variable the method takes in

• If a method requires more than one input, the input parameter declarations are separated with commas

Page 7: Writing Static Methods Up until now, we have been USING (calling) static methods that other people have written. Now, we will start CREATING our own static.

modifer returnValueType methodName(list of input parameters) {

method variables declarationmethod body

return value}

• Inside the method, you can declare variables and use them just like we have been doing in the main method. Variables declared inside of a function (or in a function’s input parameter list) can only be seen inside of the curly braces of that function.

• So if you declare a variable inside a method (like the main method), it cannot be seen inside the other methods in your class.

• Because of this, you can have 2 variables with the same name inside of the same class, but inside different methods, and containing different values.

Page 8: Writing Static Methods Up until now, we have been USING (calling) static methods that other people have written. Now, we will start CREATING our own static.

modifer returnValueType methodName(list of input parameters) {

method variables declarationmethod body

return value}

• Finally, the return value is what the function returns to whoever called it. The return value’s type must match the returnValueType listed at the beginning of the function.– You return a value by following the Java reserved word

“return” with the name of the variable whose value you want to return. For example, the following returns the value of x:

return x;– If the returnValueType is void, you cannot return anything,

but you can still use type return; to leave the method.

Page 9: Writing Static Methods Up until now, we have been USING (calling) static methods that other people have written. Now, we will start CREATING our own static.

Examples of writing and calling static methods

public class PracticeClass {

public static void main(String[] args){

int num1 = 4;int num1Cubed = PracticeClass.cube(4);System.out.println("num1 cubed is"+num1Cubed);

}//finds a3

public static int cube(int a){

int value = a*a*a;return value;

}}

Page 10: Writing Static Methods Up until now, we have been USING (calling) static methods that other people have written. Now, we will start CREATING our own static.

Review

• You have used JOptionPane.showInputDialog() to read in a number from the user as a String, then used Double.parseDouble() to turn that String into a valid double value.

• Similarly, you have used JOptionPane.showInputDialog() to read in a number from the user as a String, then used Integer.parseInt() to turn that String into a valid int value.

• Getting ints or doubles from the user in a program is a very common occurrence in first-year computer programming classes. Because of this, it might be useful to make a static method we can call whenever we want to get a number, instead of having to use both showInputDialog and parseInt/parseDouble to do the same thing.

Page 11: Writing Static Methods Up until now, we have been USING (calling) static methods that other people have written. Now, we will start CREATING our own static.