Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

Java, adding methods to native objects?

Hey guys, this may be a dumb question, but im just curious. Is there an obvious way to add methods to native objects? What i mean by that is, can i add custom methods to say String objects.

Say i want to custom create the .indexOf() method for Strings. I could obviously add a method in my class and have it look something like:

Main.indexOf(theLetter, theString)

But i would have to make this a method of Main (or whatever class i put it under). But is there a way to make it a custom method to the String object?

Ty so much in advance!

1 Answer

No, you can't do that. It has to be your own class.