140 likes | 291 Views
Static Variables and Methods. Looking at Methods. A method has the following structure: modifer returnValueType methodName (list of input parameters) { method variables declaration method body return value }
E N D
Looking at Methods • A method has the following structure: modiferreturnValueTypemethodName(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.
modiferreturnValueTypemethodName(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. • publicmeans 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);
modiferreturnValueTypemethodName(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. • Ifa 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!
modiferreturnValueTypemethodName(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
modiferreturnValueTypemethodName(list of input parameters) { method variables declaration method body return value } • The listof input parametersis a list of variable declarations that say what the method needs from the person calling it in order to work. • If a method requires more than one input, the input parameter declarations are separated with commas
Example of writing and calling static methods public class PracticeClass { public static void main(String[] args){ int num1 = 4; int solution= PracticeClass.cube(4); System.out.println("num1 cubed is"+ solution); } //finds a3 public static int cube(int numberToCube){ return numberToCube * numberToCube * numberToCube; } }
Calling static methods • When you call a static method (by saying ClassName.methodName(inputParameters), you are assigning the value you put in as input parameters to the input variables declared in the method signature //calling PracticeClass.cube(4); in the main methodsets the cube method's input parameter a = 4 public static int cube(int numberToCube){ return numberToCube * numberToCube * numberToCube; }
Calling static methods ClassName.methodName(inputValues); • ClassName is the class the method is written in. Since the pow method was originally written in the Math class, to call it we say Math.pow(4, 2); • methodName is the name of the method. You can find method names in the Java API. They almost always start with a lower-case letter. • inputValues is where you give the method what it needs in order to work. If the method takes in two or more input values, separate them with a comma. Look in the Java API to see the declaration for the input parameters (so you know what type of variable it needs in order to work).
Using static methods from the Java API • There are many static methods in the Java API that are useful: • Math.pow(3, 8); //returns 38 • Math.sqrt(36); //returns 6 • JOptionPane.showInputDialog("How are you?"); • JOptionPane.showMessageDialog(null, "Hi"); • System.currentTimeMillis() /*returns the number of milliseconds between now and January 1, 1970 */ • System.exit(0); //closes the JVM (ending the program) • The Math and System classes are imported automatically, so you don't have to do it yourself. • You can import the JOptionPane class with:import javax.swing.JOptionPane;at the very top of your program
Static variables • Just as there are static methods that we can use from other classes, there are static, "class" variables you can access from other classes to use in your programs. • To access a static "class" variable found in the API, say:ClassName.variableNamewith NO parenthesis (parenthesis are just for methods, not variables)
Static variables • Examples: • Math.PI is a constant, static double variable in the Math class, holding an approximate value of pi • Math.E is a constant, static double variable in the Math class holding an approximate value for e, the base of natural logs • System.out is a static PrintStream variable (PrintStream is an Object like String is...note the capital letters for its type) • JOptionPane.QUESTION_MESSAGE is a constant, static int variable used when JOptionPane method wants an "intoptionType" as an input parameter • In your code, you can use them like you would any other variable:double circumference = 2 * Math.PI * radius;
UsefulStatic methods • we said an int and a double are primitive data types...NOT objects. • Because some things in Java require you to use Objects, there are classes in the API called Double and Integer that we will discuss more later. We won't worry about why these classes exist for now, other than to note that they include some very useful methods to turn Strings (which you get from JOptionPane's showInputDialog method) into primitive ints or doubles: • String text = "4";int int1 = Integer.parseInt(text);//int1 has value 4double dub1 = Double.parseDouble(text);//dub1 has value 4.0
UsefulStatic methods • besides Integer.parseInt and Double.parseDouble, the Integer and Double classes have a few other static methods, as well: • System.out.println( Integer.toBinaryString(45) );/*prints out 101101, or 45 as a binary number*/ • String x = Integer.toString(45)/*converts the int 45 into the String value "45", so it can be stored into a String variable*/