简体   繁体   中英

current package keyword in java

1.Is there a keyword to refer to the current package that you are working in Java. Like we have "this" to refer to the current object. So , is there something similar for a package?

2.Also if the current class that I am working on is in a directory which has other classes,and my class has no package statement, then it will be in the default package. So, is there any way to import rest of the classes in the directory. I know that we can specify the classpath while compiling, but is there any way to do it using imports ?

  1. No, there is not
  2. No, classes in the default package can't be imported. That's one of the reasons you should never put your classes in the default package. The obvious reason is that, if every library did that, you would end up with conflicts between classes.

The first one

No keyword like this to get package name , but you can get package name by java reflection , like the following code

package com.netease.unitest.controller;

public class GenTest {
    public static void main(String[] args) {
        System.out.println(GenTest.class.getPackage().getName());

    }
}

the output is

com.netease.unitest.controller

you can get more details in the following link

http://tutorials.jenkov.com/java-reflection/index.html

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