为什么我们不能使用新关键字创建InetAddress类的对象



我在我的Java开发程序包中检查了代码InetAddress类文件,我会看到以下行。

线号:302-308

/**
     * Constructor for the Socket.accept() method.
     * This creates an empty InetAddress, which is filled in by
     * the accept() method.  This InetAddress, however, is not
     * put in the address cache, since it is not created by name.
     */
    InetAddress() {
        holder = new InetAddressHolder();
    }

在上面的代码中,我可以看到InetAddress类修饰符是default不是private,那么我们也不能使用新方法创建InetAddress类的对象。喜欢以下

InetAddress addr = new InetAddress();

您可以....但是您不应该....

记住友好的含义作为可见性概念...

如果您通过省略,疏忽或错误定义了用名称 java.net;

的包裹

然后以下

InetAddress v = new InetAddress(); 

将有效..

再次... 不是您无法做的事情...是您不应该做的事情!

最好永远不要用SDK软件包名称定义自己的包裹,否则您会得到

等例外

java.lang.securityException:禁止的包装名称:java.net

最新更新