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 Weather App (2015) Concurrency and Error Handling What To Do When the Network is Down

what is newInstance?

Hi, I already done with showing all errors in Dialog. But what does newInstance mean?

Derick Browne
Derick Browne
Courses Plus Student 6,575 Points

Is part of a syntax which make a new instance of a code(new object). Random random = new Random();

1 Answer

Jeremiah Shore
Jeremiah Shore
31,168 Points

To be a little more specific, newInstance() is a method all Objects inherit from the Object class. It can be overridden for your own uses. A specific time I have used it was with an alert dialog. I made my class AlertDialogFragment a subclass (extends) of DialogFragment.

I did this because it is I'll advised to use a non-default constructor. new AlertDialogFragment(myString, myInt) is a non-default while new AlertDialogFragment() is the default. I still needed someway to pass arguments at creation of a dialog so I could pass the title, message, and button text each time I made a dialog. Overriding the newInstance() method made that possible.

Read more about it on this treehouse forum post. Also be sure to read the stackOverflow article that was shared in the answer; linked again here