CIS 35A: Introduction to Java Programming

Home | Green Sheet | Lectures | Assignments | FAQ

Java

Java Programming
Classes, objects, and methods
Call methods

  • When you create an object from a Java class, you are creating an instance of the class. Then, you can use the methods of the class by calling them from the object.
  • Some Java classes contain static methods. These methods can be called directly from the class without creating an object.
  • When you create an object from a class, the constructor may require one or more arguments. These arguments must have the required data types, and they must be coded in the correct sequence separated by commas.
  • When you call a method from an object or a class, the method may require one or more arguments.
To call a method from an object:

Syntax
objectName.methodName(arguments)
Examples
// get a double entry from the console
double subtotal = sc.nextDouble();

// convert the date to a string
String currentDate = now.toString();
To call a static method from a class:

Syntax
ClassName.methodName(arguments)
Examples
// convert a double to a string
String sPrice = Double.toString(price);

// convert a string to a double
double total = Double.parseDouble(userEntry);
Previous | Classes, objects, and methods | Import Java classes | Create objects | Call methods | Use API documentation to research Java classes | Next