Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can new File() constructor return null on Android?

Tags:

java

android

I saw this code in android training center website:

boolean hasExternalStoragePrivateFile() {
    // Get path for the file on external storage.  If external
    // storage is not currently mounted this will fail.
    File file = new File(getExternalFilesDir(null), "DemoFile.jpg");
    if (file != null) {
        return file.exists();
    }
    return false;
}

Does that mean that on Android new File() can return null?

http://developer.android.com/reference/android/content/Context.html#getExternalFilesDir(java.lang.String)

like image 241
Cyrusmith Avatar asked Mar 18 '13 10:03

Cyrusmith


People also ask

Can a constructor return null?

Java Constructors Never Return Null.

Can C# new return null?

If the developer really needs to return a null value from the method (in 99% of cases this is not necessary), the return type can be marked as a nullable reference type (the feature was introduced in C# 8.0).

How do you return a null object in Java?

In Java, a null value can be assigned to an object reference of any type to indicate that it points to nothing. The compiler assigns null to any uninitialized static and instance members of reference type. In the absence of a constructor, the getArticles() and getName() methods will return a null reference.


1 Answers

No, a constructor may never return null. A constructor either returns a fully initialised object or throws an exception or error.

like image 121
Joni Avatar answered Sep 27 '22 17:09

Joni