Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

string

I'd like to add a method AddDefaultNamespace() to the String class in Java so that I can type "myString".AddDefaultNamespace() instead of DEFAULTNAMESPACE + "myString", to obtain something like "MyDefaultNameSpace.myString". I don't want to add another derived class either (PrefixedString for example).

Maybe the approach is not good for you but I personally hate using +. But, anyway, is it possible to add new methods to the String class in Java?

Thanks and regards.

like image 542
user15546 Avatar asked Sep 17 '08 10:09

user15546


People also ask

How do you add methods to a class in Java?

Remember that.. The dot ( . ) is used to access the object's attributes and methods. To call a method in Java, write the method name followed by a set of parentheses (), followed by a semicolon ( ; ). A class must have a matching filename ( Main and Main.

How many methods are there in String class in Java?

Strings are a sequence of characters and are widely used in Java programming. In the Java programming language, strings are objects. The String class has over 60 methods and 13 constructors.

Can we have two methods in a Java class?

What is Overloading? For convenience, Java allows you to write more than one method in the same class definition with the same name. For example, you can have two methods in ShoppingCart class named computeCost. Having two or more methods named the same in the same class is called overloading.

Which of the method is not there in String class?

So because of this String class not having reverse() method. No. One could transfer all characters to a char[] array, reverse the array, then create a String from the resulting array. No intermediade String objects required.


1 Answers

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

like image 102
GustyWind Avatar answered Sep 27 '22 22:09

GustyWind