Java question about creating objects inside of other classes. via /r/learnprogramming


Java question about creating objects inside of other classes.

Why is it when I see classes using/creating objects within said class, they're typically instantiated in the constructor instead of the object variable? Ex:

public class Factory{ ArrayList<String> list; public Factory(){ list=new ArrayList<String>(); } } 

instead of:

public class Factory{ ArrayList<String> list=new ArrayList<String>(); public Factory(){} } 

Does it matter how its created? Is the first example better than the second somehow?

Submitted July 17, 2017 at 05:02PM by Adeloastro
via reddit http://ift.tt/2ut4970

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s