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
Creating a Method A method has the following structure: modifer returnValueType methodName(list of input parameters) { method variables declaration method 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.
modifer returnValueType methodName(list of input parameters) { method variables declaration method 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);
modifer returnValueType methodName(list of input parameters) { method variables declaration method 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!
modifer returnValueType methodName(list of input parameters) { method variables declaration method 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
modifer returnValueType methodName(list of input parameters) { method variables declaration method 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
modifer returnValueType methodName(list of input parameters) { method variables declaration method 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.
modifer returnValueType methodName(list of input parameters) { method variables declaration method 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.
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 a 3 public static int cube(int a) { int value = a*a*a; return value; }
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.