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.