How to Add New Methods to the String Class in Java

Can I add new methods to the String class in Java?

String is a final class which means it cannot be extended to work on your own implementation.

Is it possible to add customize method in String class?

If you really intend to add a new method to java.lang.String.java then you are probably on a wrong path which will only cause pain and agony in future :d. Better stop, rethink redesign and refactor.

Anyways firstly NO you can not add a new method to String class. It is final and hence you can not and neither you should even intend to sub class it. If you intend to do so then its a poor design, which you must refactor.

If you just wish to check if some String is present in the database, you can simply declare the method which can accept a parameter of type String , check its presence in the database and return true if found else return false.

If above solution does not work for you then you can try takimg help of Composition. If you want a Object with such method (which can tell if the object is present in db) then You can create a class , may decide to name it as per your contextual needs , I am naming it as StringContainer. This class can have a instance variable of type String. Now instead of using String object you can use the object of this newly created custom class composing the object of String You can include a method to check if entry cossponding to an object of this class had been made in database or not.

Add method .equalsOr() to String class

If you want to check equality of your string1 to a set of strings string2, string3, string4, string5. It's better to use any collection (i.e. Set or List). For example, if you use

 Arrays.asList(string2, string3, string4, string5).contains(string1)

it will give you the espected result.

Instance methods in String Class in java

A string literal like "myString" does create a String object¹, even if you don't explicitly see a new String() call in the code. Consider String s = "myString"; which couldn't work otherwise! So you can call any method on it as usual.


¹ Or reuses an existing one, see string pooling.

Add new method to existing class

First of all, the title of the question is wrong. You did not "Add new method to existing class". You added a method to an anonymous class that extends an existing class.

Anonymous classes can contain any methods that regular classes do. They don't have to only contain methods of the class they extend or the interface they implement.

Therefore, defining sayHello2 in your anonymous class is perfectly valid, but quite useless, since you can't call that method from outside the anonymous class body.

On the other hand, it can be useful if you do call it from within the anonymous class body (and you can make it private, since it can't be called from outside anyway):

User s = new User("CHORT") {
@Override
public void sayHello() {
sayHello2();
}
private void sayHello2() {
System.out.println("HELLO FROM ANONYMOUS CLASS");
}
};

EDIT: Another thing I thought about - you can probably use reflection to call your sayHello2 from outside, though I'm not sure why you'd ever want to do that.



Related Topics



Leave a reply



Submit