简体   繁体   中英

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

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.

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).

There is no difference. The second example is a factory method.

You pass in a few parameters and that method will call new at some point on the actual instance class of the Path .

While it behaves like a constructor, there are also differences which should be pointed out: Static factory methods do not have to return the current type, but can also return a subtype, where in contrast a constructor creates an instance of the current class. (Hypothetical) Example:

public static Path create(String name) {
    return new AbsolutePath(name); // subclass/implementation of Path
}

From an implementation point, this gives you a lot of flexibility for later extensions. You can for example implement some logic, which decides which concrete type to create within the method. You could cache instances and return them. You could return the same instance every time (Singleton). Etc.

Further aspect: You can actually give meaningful names to static factory methods, so code is easier to read:

public static Path createAbsolute(String name) { ... }
public static Path createRelative(String name) { ... }

With the first option you are sure you are creating a new object (more or less, java.lang.* classe are a bit special) Let's take the second option:

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

Nothing assures you the instance you are storing in filePath is a Path one, it can be an instance of a subclass of Path. Something similar occurs with Calendar: Calendar is an abstract class, so

Calendar c=Calendar.getInstance();

The variable c is actually a GregorianCalendar .

Another difference:

class Singleton {

    private Singleton s=null;

    private Singleton(){};

    public static Singleton getSingleton() {
        if (s==null) {
            s=new Singleton();
        }
        return s;
    }
}

No matter how many times you call getSingleton , you will only create one object.

when you are using new keyword then an object of the particular class is created.

Here Thing someThing = new Thing(); something is an object of Thing class

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

Path is a class having static method get() which accepts String arguments and it returns Path something like

public static Path get(String arg)
{
return path;
}

The memory is allocated by the method call to Path.get in the second instance. This allows the library to go through its own initialisation routines for a Path variable and which may perform additional checks. New just allocates memory. The memory may also be sorted and stored internally in some structure too, such that it doesn't constantly reload the same object via caching. I, personally, always call the factory methods rather than new up an object myself, however it could be considered to be a style thing, as pretty much everything that may be done with a factory method may also be achieved via a constructor.

In your examples, you are assuming that an object is created without a "new". That is an incorrect assumption. The object was created with "new" in the second example as well.

Just because you can't see the "new" doesn't mean it's not called in the function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM