Continuing our Java/J2EE/JEE interview questions and answers series, today's questions are:
Q Can anonymous class have static members?
With one exception, anonymous classes cannot contain static fields, methods, or classes. The only static things they can contain are static final constants.
Q When I create anonymous classes, what access modifier do they get?
Anonymous classes get the default, unnamed access modifier. You cannot make them private, protected, public, or static.
Q How do I create a constructor for an anonymous class?
Anonymous classes don't have names, so you cannot create a constructor by just using the name of the class. What you can do instead is to use an instance initializer. Instance initializers look like methods with no names or return values:
ActionListener myActListen = new ActionListener {
{
System.out.println("Instance Initializer");
}
public void actionPerformed(ActionEvent event) {
// ...
}
};
Q When should I use a "private" constructor?
The most common use that for private (and protected) constructors is
in implementing singletons and some types of object factories. To do this, you use
a static method to manage the creation of one (or more) instances of the class.
Q How do I get a list of the files in a .zip file from Java?
You can get a java.util.Enumeration of java.util.zip.ZipEntry objects by
using the java.util.zip.ZipFile.entries() method.
Q What is a JIT compiler?
A Just-In-Time compiler is one way to implement the Java Virtual Machine. Rather than the typical, pure interpreter, a JIT compiler will, on demand, compile the platform independent Java byte codes into platform specific executable code, in real-time. For first time byte codes are compiled before being executed but for future passes cached version of compiled code is used . This tends to result in a performance increase of 10-100 times, depending upon the task at hand, over purely interpreted byte codes.
Previous Java J2EE Interview Questions can be read here.
0 Responses to Java Interview Questions -3
Something to say?