Java - Using Accessor and Mutator Methods

I am working on homework. I am confused about how this should be done.

The question arises:

Create a class called IDCard that contains the username, ID number, and name of the file containing the person photogrpah. Write accessor and mutator methods for each of these fields. Add after two overloaded class constructors:

public identification card IDCard () (String n, int ID, String filename)

Test your program by creating various ojbect using these two constructors and print their values ​​on the console using accessor and mutator methods.

I rewrote this so far:

public class IDCard { String Name, FileName; int ID; public static void main(String[] args) { } public IDCard() { this.Name = getName(); this.FileName = getFileName(); this.ID = getID(); } public IDCard(String n, int ID, String filename) { } public String getName() { return "Jack Smith"; } public String getFileName() { return "Jack.jpg"; } public int getID() { return 555; } } 
+4
source share
2 answers

Let's move on to the basics: "Accessor" and "Mutator" are just fancy names for getters and setters. The getter, "Accessor", returns a class variable or its value. Setter, "Mutator", sets the pointer to a class variable or its value.

So, first you need to set up a class with some variables to get / set:

 public class IDCard { private String mName; private String mFileName; private int mID; } 

But oh no! If you instantiate this class, the default values ​​for these variables will be meaningless. BTW "instance" is a fancy word for doing:

 IDCard test = new IDCard(); 

So - configure the default constructor, this is the method called when the class is "instantiated".

 public IDCard() { mName = ""; mFileName = ""; mID = -1; } 

But what if we know the values ​​that we want to give to our variables? Therefore, let's create another constructor that takes parameters:

 public IDCard(String name, int ID, String filename) { mName = name; mID = ID; mFileName = filename; } 

Wow is good. But stupid. Because we have no way to access (= read) the values ​​of our variables. So, add a getter, and while we are on it, add a setter:

 public String getName() { return mName; } public void setName( String name ) { mName = name; } 

Nice. Now we can access mName . Add the rest of the accessories and mutators, and you are now a certified Java newbie. Good luck.

+31
source

You need to remove static from your access methods - these methods must be instance methods and access instance variables

 public class IDCard { public String name, fileName; public int id; public IDCard(final String name, final String fileName, final int id) { this.name = name; this.fileName = fileName this.id = id; } public String getName() { return name; } } 

You can create an IDCard and use it as follows:

 final IDCard card = new IDCard(); card.getName(); 

Each time you call new , a new instance of IDCard will be created and it will have its own copies of the three variables.

If you use the static , then these variables are common to all IDCard instances.

A few things to keep in mind:

  • do not add useless comments - they add a mess of code and nothing more.
  • conform to naming conventions, use lowercase variable names - name not name .
+2
source

Source: https://habr.com/ru/post/952502/


All Articles