File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Loading user defined class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Loading user defined class" Watch "Loading user defined class" New topic

Loading user defined class

siva prakash
Ranch Hand

Joined: Aug 24, 2004
Posts: 80
Hi All,

Can i override my own String class in place of sun's provided String class and use as normally .

ie code snippet will be like this,

class String {

String toString() {
// my own functionality
// some methods as in normal String class( provided by sun)

public class Main {

String st ; // here my own implementation should be used not sun's


Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
When you define a class called String you're not overriding anything, you've
just happened to give your class the same name as a class in another package,
namely java.lang.String.

Now *is it a good idea* to do this? Most programmers have such universal
classes hardwired into their brains: String == java.lang.String, so I'd
suggest calling your class something else, like MyString...

This reminds me of the time I was trying to help a student of mine who
had a mysterious error in his cloning code -- the interface Cloneable
just didn't seem to be working right. Then I discovered he had inadvertantly (?)
define his own interface Cloneable!

Also note, by the way, that you can't subclass java.lang.String, not matter
what you call the subclass -- String is final.
[ December 15, 2005: Message edited by: Jeff Albrechtsen ]

There is no emoticon for what I am feeling!
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
What requirement are you trying to satisfy by doing this? It appears all you want to do is override toString() which you can do in any object.
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24195

Originally posted by siva prakash:
Can i override my own String class in place of sun's provided String class and use as normally .

The short answer is: Yes, actually. You need to make sure the compiled class is on the special boot class path, rather than on the normal classpath. You'd want to start Java using something like

java -bootclasspath c:/mydir;c:/jdk1.5.0/jre/lib/rt.jar MyProgram

and then make sure that java/lang/String.class was in "c:/mydir".

The longer answer is that if you're writing a special tool -- a debugger, or profiler, or something else that has to do weird things -- then this might be called for. Or if you're doing this temporarily while hacking on a program to instrument something to help you understand what's going on, then that's fine: I've done that kind of thing myself.

But in normal programming, it's terrible style; and of course in many environments you won't even be able to do it, because you won't have access to the boot classpath.

[Jess in Action][AskingGoodQuestions]
jQuery in Action, 3rd edition
subject: Loading user defined class
It's not a secret anymore!