Concept
How do you use methods defined in another Java class?
ComputerScienceOne / Using Methods
"Once a method has been defined in a class, you can make use of the method as follows. First, you may need to import the class itself depending on where it is. For example, suppose that the examples we’ve presented so far are contained in a class named Utils (short for “utilities”) which is in a package named unl.cse . Then in the class in which we want to call some of these functions we would import it using import unl.cse.Utils; prior to the class declaration. Once the class has been imported, we can invoke a method in the class by first referencing the class and using the dot operator to access one of its methods. For example,\n\n1 int a = 10, b = 20;\n\n2 int c = Utils.sum(a, b); //c contains the value 30\n\n3\n\n4 //invoke a method with literal values:\n\n5 double dist = Utils.getDistance(0.0, 0.0, 10.0, 20.0);\n\n6\n\n7 //invoke a method with a combination:\n\n8 double p = 1500.0;\n\n9 double r = 0.05;\n\n10 double monthlyPayment = Utils.getMonthlyPayment(p, r, 60);"
Related Ideas
- How do you use methods defined in another Java class?ComputerScienceOne · Void Methods
- Why is the Utils.methodName() syntax used for methods?ComputerScienceOne · Passing By Reference
- How do member methods differ from static methods?ComputerScienceOne · Accessor & Mutator Methods
- How do you use Java instances and invoke their methods?ComputerScienceOne · Usage
- How are Java class instances created and used?ComputerScienceOne · Common Methods
- How do you declare and instantiate Java class instances?ComputerScienceOne · Usage
- How do accessor and mutator methods use this to access instance variables?ComputerScienceOne · Accessor & Mutator Methods
- How are Java classes organized and stored in source files?ComputerScienceOne · Data Visibility