Q 04: 怎么用Java里的Package?
A 04: 利用Java的package,因类同名而造成的冲突就可以轻易地解决掉.这个package的概念也有利于帮助我们的整理一个项目中的文件.
Java里的java.lang包是自动引入的,而其它的包就得手动用import引入了.
Q 05: 说说你对类加载器的理解?如何来运行一个包中的类?说说对动态类加载器的理解?
A 05: 类加载器是分层地布署的(class loaders are hierachical).在JVM中,类是以这种方式加载进去的,当这个类在一个已运行的类中有引用时,就会由JVM加载进去.说到这里,就会有人问了,那么第一个类是怎么加载进来的呢?第一个类是在自写类的声明的那个static main方法加载进来的.... 一个类加载器会创造一个namespace.所有的JVM中都至少包含一个类加载器,这个加载器就是内置在JVM里的primordial(或bootstrap)类加载器.下面我来看那些non-primodial类加载器.JVM中也另有一个接口可以让用户自定义的类加载器来替代那个primordial类加载器.下面是由JVM创建的类加载器:
CLASS LOADER | RELOADABLE | EXPLANATION |
Bootstrap(primordial) | No | 加载JDK自带的那些类,即java.* packages里的类.(这个由sun.boot.class.path这个系统属性配置的.也就是 加载那些rt.jar和i18n.jar) |
Extensions | No | 加载JDK扩展目录下的那些jar包(这个由java.ext.dirs 系统属性配置--通常是JRE下的lib/ext目录) |
System | No | 加载系统classpath里配置的类(通常由java.class.path属性定义,这个由系统的ClassPath环境变量或命令提示行 里的-classpath或-cp指定.) |
见上传的图片:JVM class loaders
Bootstrap(primordial)(rt.jar,i18n.jar) -->Class loaded by Bootstrap class loader have no visibility into classes loaded by its descendants(ie Extentions and System class loaders).(有了这个visibility后又会有什么用呢?)
Extensions(lib/ext) -->The classes loaded by system class loader have visibility into classes loaded by its parents(ie Extensions and Bootstrap class loaders).那这里加载的类有了visibility后又会怎么样呢?这里的Extensions类是"javax."这样的标记的么?那么举了例子javax.sql.ConnectionEvent类加载到JVM后有了对这个类"java.sql.Time"的visibility后又会有什么好处呢?在实际编程中,怎么又一点也没有体会这个呢,更没有用到上这个特性.
System(-classpath) --> If there were any sibling class loaders they(这个"they"是指那个"sibling class loaders"吧?是说一个sibling class loader就不能触及到由其它的sibling class loader加载的类?) cannot see classes loaded by each other.They can only see the classes loaded by their parent class loader.For example Sibling1 class loader cannot see classes loaded by Sibling2 class loader.
Sibling1 classloader or Sibling2 classloader: -->Both Sibling1 and Sibling2 class loaders have visibility into classes loaded by their parent class loaders(eg: System,Extensions,and Bootstrap).
Class loaders are hierachical and use a delegation model when loading a class.Class loaders request their parent to load the class first before attemping to load it themselves.When a class loads a class,the child class loaders in the hierachy will never reload the class again.Hence uniqueness is maintained.Classes loaded by a child class loader have visibility into classes loaded by its parents up the hierachy but the reverse is not true as explained in the above diagram.
Q. what do you need to do to run a class with a main() method in a package?
Important: Two objects loaded by different class loaders are never equal even if they carry the same values,which mean a class is uniquely identified in the context of the associated class loader.This applies to singletons too, where each class loader will have its own singleton.
Q. 说说你对动态加载与静态加载的区别?
static class loading | dynamic classl oading |
类的静态加载是通过Java里的关键字"new"来完成的: Class MyClass{ public static void main(String[] args){ Car c = new Car(); } } | Dynamic loading is a technique for programmatically invoking the functions of a class loader at run time. Let us look at how to load classes dynamically. Class.forName (String className); //static method which returns a Class The above static method returns the class object associated with the class name. The string className can be supplied dynamically at run time. Unlike the static loading, the dynamic loading will decide whether to load the class Car or the class Jeep at runtime based on a properties file and/or other runtime conditions. Once the class is dynamically loaded the following method returns an instance of the loaded class. It’s just like creating a class object with no arguments. class.newInstance (); //A non-static method, which creates an instance of a //class (i.e. creates an object). Jeep myJeep = null ; //myClassName should be read from a .properties file or a Constants class. // stay away from hard coding values in your program. CO String myClassName = "au.com.Jeep" ; Class vehicleClass = Class.forName(myClassName) ; myJeep = (Jeep) vehicleClass.newInstance(); myJeep.setFuelCapacity(50); |
A NoClassDefFoundException is thrown if a class is referenced with Java’s “new” operator (i.e. static loading)but the runtime system cannot find the referenced class. | A ClassNotFoundException is thrown when an application tries to load in a class through its string name using the following methods but no definition for the class with the specified name could be found:
|
Q. What are "static initializers" or "static blocks with no function names"? When a class is loaded, all blocks
that are declared static and don’t have function name (i.e. static initializers) are executed even before the
constructors are executed. As the name suggests they are typically used to initialize static fields. CO
public class StaticInitializer {
public static final int A = 5;
public static final int B; //note that it is not Æ public static final int B = null;
//note that since B is final, it can be initialized only once.
//Static initializer block, which is executed only once when the class is loaded.
static {
if(A == 5)
B = 10;
else
B = 5;
}
public StaticInitializer(){} //constructor is called only after static initializer block
}
The following code gives an Output of A=5, B=10.
public class Test {
System.out.println("A =" + StaticInitializer.A + ", B =" + StaticInitializer.B);
}