This essay developed out of conversations I've had with several other programmers about why Java smelled suspicious. It's not a critique of Java!
Sycorax - complete tutorials
Programming Tutorials
" Java provides the industry - software companies and customer alike , an opportunity to create a true open computing environment where software is portable,
and customers benefit from increase competition. "
Java and the Future
December 1, 2008-LEJB 3.1: EJB New and Improved!
The EJB 3.0 specification was a huge improvement from what you were used to in the early versions of EJB. Available as an early draft, EJB 3.1 has many more features and is even easier to use.
December 1, 2008-Should Java Assert that Network I/O Can't Occur on the UI Thread?
Doing network I/O on the user interface (UI) thread is bad. Most developers know that and can tell you why; unfortunately, it's still done.
Register now to recieve special alert and latest technology news!
Core Java Interview Question
What are inner classes?
classes defined in other classes, including those defined in methods are called inner classes. An inner class can have any accessibility including private.
Inner classes are class files whose "name" is scoped to be inside another class. What do I mean by "scope"? The scope of a name defines when that name can be referenced by Java code. When two classes share the same package, they are said to have "package scope." Classes that are outside the containing package cannot refer to classes inside the package that are not declared as being public. Thus the names of the classes are scoped to the package.
Inner classes are scoped to the class used to declare them -- thus they are effectively "invisible" to the other classes in the same package. This lack of visibility to other classes in the package gives the programmer the opportunity to create a set of classes within a containing class without cluttering up the name space of classes in their package
The inner classes are actually compiled to regular class files, only the names are changed to prevent them from colliding with existing classes.
The object-oriented advantage of inner class?
In my humble opinion, the most important feature of the inner class is that it allows you to turn things into objects that you normally wouldn't turn into objects. That allows your code to be even more object-oriented than it would be without inner classes.
Let's look at the member class. Since its instance is a member of its parent instance, it has access to every member and method in the parent. At first glance, this might not seem like much; we already have that sort of access from within a method in the parent class. However, the member class allows us to take logic out of the parent and objectify it. For example, a tree class may have a method and many helper methods that perform a search or walk of the tree. From an object-oriented point of view, the tree is a tree, not a search algorithm. However, you need intimate knowledge of the tree's data structures to accomplish a search.
Types of inner class?
In Java there are four types of nested class:
Static member classes - static member class is a static member of a class. Like any other static method, a static member class has access to all static methods of the parent, or top-level, class.
Member classes - a member class is also defined as a member of a class. Unlike the static variety, the member class is instance specific and has access to any and all methods and members, even the parent's this reference.
Local classes - Local classes are declared within a block of code and are visible only within that block, just as any other method variable.
Anonymous classes - an anonymous class is a local class that has no name.
In Java, a static inner class may be called a 'nested class'.
Anonymous classes?
Anonymous class is a class defined inside a method without a name and is instantiated and declared in the same place and cannot have explicit constructors.
When you don't even need a name because you really just want to pass a method that does something (analogous to a C language callback function), then you can get away with creating an anonymous inner class. These classes are simply inner classes that are not given a specific name. Typically, a class is not named when it is used only once.
public class InnerClass1 {
String msg = "Hello";
void doWork() {
Inner i = new Inner();
i.doTheWork();
msg = "Goodbye";
i.doTheWork();
}
public static void main(String[] av) {
InnerClass1 p = new InnerClass1();
p.doWork();
}
class Inner {
public void doTheWork() {
// print member of enclosing class
System.out.println(msg);
} } }
Inheriting an inner class?
class WithInner {
class Inner {
} }
public class InheritInner extends WithInner.Inner {
//! InheritInner() {} // Won't compile
InheritInner(WithInner wi) {
wi.super();
}
public static void main(String[] args) {
WithInner wi = new WithInner();
InheritInner ii = new InheritInner(wi);
}
An inner class cannot be overriden like a method?
class Egg {
private Yolk y;
protected class Yolk {
public Yolk() {
System.out.println("Egg.Yolk()");
} }
public Egg() {
System.out.println("New Egg()");
y = new Yolk();
} }
public class BigEgg extends Egg {
public class Yolk {
public Yolk() {
System.out.println("BigEgg.Yolk()");
} }
public static void main(String[] args) {
new BigEgg();
} }