Concept
How do member methods differ from static methods?
ComputerScienceOne / Accessor & Mutator Methods
"In contrast to the methods we defined in Chapter 29, when defining a member method, we do not use the static keyword. Making a variable or a method static means that the method belongs to the class and not to instances of the class. We add to our example by providing two public methods that compute and return a result on the member variables. We also use Javadoc style comments to document each member method.\n\n1 package unl.cse;\n\n2\n\n3 public class Student {\n\n4\n\n5 //member variables:\n\n6 private String firstName;\n\n7 private String lastName;\n\n8 private int id;\n\n9 private double gpa;\n\n10\n\n11 /**\n\n12 * Returns a formatted String of the Student's\n\n13 * name as Last, First.\n\n14 */\n\n15 public String getFormattedName() {\n\n16 return lastName + \", \" + firstName;\n\n17 }\n\n18\n\n19 /**\n\n20 * Scales the GPA, which is assumed to be on a\n\n21 * 4.0 scale to a percentage.\n\n22 */\n\n23 public double getGpaAsPercentage() {\n\n24 return gpa / 4.0;\n\n25 }\n\n26\n\n27 }"
Related Ideas
- Why is the Utils.methodName() syntax used for methods?ComputerScienceOne · Passing By Reference
- How do you use Java instances and invoke their methods?ComputerScienceOne · Usage
- What does the completed Java Student class example contain?ComputerScienceOne · Example
- How do you use methods defined in another Java class?ComputerScienceOne · Using Methods
- How are Java class instances created and used?ComputerScienceOne · Common Methods
- What is a void method in Java?ComputerScienceOne · Void Methods
- How can method references and Comparator methods make ordering more concise?ComputerScienceOne · Java 8: Lambda Expressions
- How do accessor and mutator methods provide access to private member variables?ComputerScienceOne · Accessor & Mutator Methods