javaclassoopcompiler-errorsstatic

Can't create instance - Constructor has private access


I have tried to create instance of Desktop Class using Desktop desktop = new Desktop(); but i get the error Desktop() has private access in Desktop but In java documentation desktop class declared as public.like this..

public class Desktop extends Object

so since it is public. and neither static. so why cant I create a instance of it? I know I can use Desktop desktop = Desktop.getDesktop(); But I cant understand why I cant create a instance of Desktop class.


Solution

  • Take a look at the documentation:

    The Desktop class allows a Java application to launch associated applications registered on the native desktop to handle a URI or a file.

    The Desktop objects represents the user's native desktop. It's not a "normal" object that you could create many instances of, because there is only one desktop and it must be linked to many things of the operative system. You shouldn't worry about creating this object. The java libraries will take care of creating this object and hooking it up to the operative system.

    This is why you can't create an instance. And the way that developers of this class ensure that you can't create instances of this class is to set the constructor as private. That's what the error Desktop() has private access in Desktop means.