Bifalgorithm post · Computer science one · Accessor & Mutator Methods

How do member methods differ from static methods?

How do member methods differ from static methods? (Computer science one · Accessor & Mutator Methods) — "In contrast to the methods we defined in Chapter 29, w…

Ideas in this flowchart

  • Defining member methods in the Student class
  • Unlike the methods in Chapter 29, member methods are defined without the static keyword.
  • A static method or variable belongs to the class rather than to its instances.
  • The example defines a public Student class with member variables.
  • The member variables store the student's first name, last name, ID, and GPA.
  • The class provides two public member methods that compute and return results from member variables.
  • getFormattedName returns the student's name in the format Last, First.
  • getGpaAsPercentage converts a GPA assumed to use a 4.0 scale into a percentage.
  • Javadoc-style comments document each member method.
Feed/

How do member methods differ from static methods?

No cover

Channel

Computer science one

1 page

"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 }"

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.