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

Android Build a Simple Android App (retired 2014) Learning the Language Objects and Random Numbers

habib kazemi
habib kazemi
2,041 Points

I dont understand the word 'new' in codes.

what the word 'new' in codes mean getAnswerButton.setOnClickListener(new View.OnClickListener()

2 Answers

The keyword new in Java is used to allocate memory to the objects, in short it creates the object

You will find a very precise answer here StakOverflow - What does new keyword do in Java

Hope this helps. Ask again if you doubts

Harry James
Harry James
14,780 Points

The new keyword actually creates a new instance of an object. So, in theory, you're creating a new object. Be careful when using this as, you don't want to create many instances if they're not needed. This will just waste resources on the Android device. If, however, you have an object that can only be used once, a new keyword would be required to create a new instance of it. On the other hand, if you have something like an animation, you wouldn't create a new animation each time the user tapped a button for example (Unless the animation was deleted after its completion). Instead, you would have it so the animation would restart on the same instance.

Hope this clarifies things!