CIS 35A: Introduction to Java Programming

Home | Green Sheet | Lectures | Assignments | FAQ | Grades

Classes

Objects and Classes
Static fields and methods
Call

How to call static fields and methods

  • To call a static field, type the name of the class, followed by the dot operator, followed by the name of the static field.
  • To call a static method, type the name of the class, followed by the dot operator, followed by the name of the static method, followed by a set of parentheses.
  • If the method you're calling requires arguments, code the arguments within the parentheses, separating multiple arguments with commas.

The syntax for calling a static field or method

className.FINAL_FIELD_NAME
className.fieldName
className.methodName(argumentList)

How to call static fields

From the Java API

Math.PI

From a user-defined class

FinancialCalculations.MONTHS_IN_YEAR
Product.objectCount  // if objectCount is declared as public

How to call static methods

From the Java API

NumberFormat currency =
    NumberFormat.getCurrencyInstance();
int quantity = Integer.parseInt(inputQuantity);
double rSquared = Math.pow(r, 2);

From user-defined classes

double futureValue =
    FinancialCalculations.calculateFutureValue(
    monthlyPayment, yearlyInterestRate, years);
int productCount = Product.getObjectCount();

A statement that calls a static field and a static method

double area = Math.PI * Math.pow(r, 2); // pi times r squared
Previous | Code | Call | Initialization block | Use | Next