Creating your own Method?

16,255

Solution 1

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.

Solution 2

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);

Solution 3

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.

Share:
16,255
user2388169
Author by

user2388169

Updated on June 04, 2022

Comments

  • user2388169
    user2388169 almost 2 years

    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.

  • 11684
    11684 almost 11 years
    Perfect. I did not know String was final though, otherwise I wouldn't have bothered.
  • user2388169
    user2388169 almost 11 years
    So for this example, instead of adding 'knife' to a String, I could just create a whole new class called Knfie? Then I could just say Knife knife = new Knife(); and then manipulate it that way?
  • Vishy
    Vishy almost 11 years
    Correct. In Java the solution to a problem often involves creating a custom class. This is a bit strange as not all programming languages work this way. The benefit is strict type checking, less error prone code and better performance. (It means more work for you though ;)
  • user2388169
    user2388169 almost 11 years
    Its like you picked up the source of why I came to this question, and just answered any further questions I could have. Creating a weapon class was going to be the next question I asked and you took the extra step to cover that without knowing if I would actually need it to not. Thank you for the elegant example.