user2388169
user2388169

Reputation: 237

Creating your own Method?

I might have used the wrong word, so if I did, please tell me so I can correct it. I'm just going by what I think it would be called, based on what I gathered from the Java Hierarchy.
But here is my question:

For this example, I will explain what I see, ask the question, and then provide an example. (I will be using the word Method because that's the word I think is appropriate, unless told otherwise)

Do you know how you can have a variable, and for the sake of this example, it's a String? Now for this string, you could use a method like myString.setText("String");, or myString.equalsIgnoreCase(otherString);.

Now, for my question, is it actually possible to create your own method?

For the sake of this example, let's say myString.setText("Knife");. Now since we know myString is a knife, could I create a method where I could call myString to see if it was equipped, or to make it equipped? For example: myString.setEquipped(true); and if I wanted to check if it was equipped or not, I could call myString.getEquipped();

Am I using the word Method properly? And if my original question is possible, could we provide a small example?

Thanks in advance for all your time and work.

Upvotes: 2

Views: 17368

Answers (3)

Henry Keiter
Henry Keiter

Reputation: 17168

I think you're just looking for the basics of object-oriented programming as they are implemented in Java (and yes, "method" is correct.) You want to define an object with some methods that modify its internal state. For a very simple example:

public class MyObject {
    // Instance variables
    private String text;
    private boolean equipped;

    public MyObject() {
        // Default constructor
    }

    // setters
    public void setText(String text) {
        this.text = text;
    }
    public void setEquipped(boolean equipped) {
        this.equipped = equipped;
    }

    // getters
    public String getText() {
        return this.text;
    }
    public boolean isEquipped() {
        return this.equipped;
    }

    // main method
    public static void main(String[] args) {
        MyObject o = new MyObject();
        o.setEquipped(true);
        o.setText("Knife");
        System.out.println("Is 'o' equipped?");
        System.out.println(o.isEquipped());
        System.out.println("What is 'o'?");
        System.out.println(o.getText());
    }
}

You can't modify the String class, as others have said, because it is final. However, you can (and should!) define your own classes, with all the methods and objects that they need to do what they need to do. For instance, in the example you gave, you wanted to be able to set the object's text and then tell whether it is equipped. That suggested to me that you want a class where each instance of the class contains a String (text) and a boolean (equipped)

That's how I chose to write my example the way that I did: your object wants a "has-a" relationship to both String and boolean. This is in contrast to an "is-a" relationship, since it wouldn't make sense for your object to actually be either a String or a boolean.

However, it might make sense for this object to be a Weapon, for instance. In that case, you would want to define a Weapon class, and then extend that class with a Knife class, a Sword class, and so on.

Upvotes: 3

Peter Lawrey
Peter Lawrey

Reputation: 533510

Yes,, you can do all that, but I suggest you use your IDE to generate these methods. e.g. If you have.

public class Item {
    private final String name;
    private boolean equiped;
}

Your IDE can generate these methods for you.

public class Item {
    private final String name;
    private boolean equiped;

    public Item(String name) {
        this.name = name;
    }

    public boolean isEquiped() {
        return equiped;
    }

    public void setEquiped(boolean equiped) {
        this.equiped = equiped;
    }

    public String getName() {
        return name;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Item item = (Item) o;

        if (equiped != item.equiped) return false;
        return !(name != null ? !name.equals(item.name) : item.name != null);
    }

    @Override
    public int hashCode() {
        int result = name != null ? name.hashCode() : 0;
        result = 31 * result + (equiped ? 1 : 0);
        return result;
    }

    @Override
    public String toString() {
        return "Item{" +
                "name='" + name + '\'' +
                ", equiped=" + equiped +
                '}';
    }
}

You can use it like this

Item knife = new Item("Knife");
knife.setEquiped(true);
System.out.println("Item is " + knife);

Upvotes: 4

djechlin
djechlin

Reputation: 60768

Only via

  1. reflection, which is not what you really want to do
  2. Modifying the original source. For most purposes you can't modify the source of String.

Then there's inheritance in which you derive a class and add methods. Most classes you can, but String is not one of them as it is final.

Upvotes: 2

Related Questions