Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the difference in java objects created with "new" and the ones that do not use "new"

Tags:

java

What is the difference between creating an object with and without "new"?

example:

Thing someThing = new Thing();

vs.

Path filePath = Path.get("C:\\......)

In the first example I understand that when instantiating the object, that "new" is allocating memory for a the someThing object and that the memory location is referenced by someThing.

My text book says " You create a Path object" by using the second example. Is the difference just how the object is stored or memory is allocated? I am not sure why you would create an object this way.

like image 834
user3137110 Avatar asked Nov 29 '22 01:11

user3137110


1 Answers

In the second case you are using a static method which is internally creating the object or passing a reference to an existing object. This is a common pattern particularly when the APIs wish to hide an internal implementation (as is the case here).

like image 161
Martin Serrano Avatar answered Dec 06 '22 08:12

Martin Serrano