java in the inner_Inner classes in Java, the mystery within.

Inner classes, also called Nested Classes, are nothing but classes that are defined within other classes. The nesting is a relationship between classes, not objects.

Inner classes have clearly two benefits, name control & access control. In Java, this benefit is not as important because Java packages give the name control.

Java inner classes have feature that makes them richer and more useful. An object of an inner class has an implicit reference to the outer class object that instantiated it. Through this pointer, it gains access to any variable of the outer object. Only static inner classes don’t have this pointer. It is actually invisible when we write the code, but compiler takes care of it. Inner classes are actually a phenomenon of the compiler and not the JVM.

Inner classesmay be defined with following access modifiers : public,protected,private, or with default package access.

The syntax for inner class is as follows:

[modifiers] class OuterClassName {

code...

[modifiers] class InnerClassName {

code....

}

}

Inner Classes:

Following properties can be noted about Inner classes:

The outer class (the class containing the inner class) can instantiate as many number of inner class objects as it wishes, inside it’s code.

If the inner classis public & the containing class as well, then code in some other unrelated class can as well create an instance of the inner class.

In above case the inner classcan be created as follows:

outerObj = new (arguments);

outerObj. innerObj = outerObj.new (arguments);

No inner class objects are automatically instantiated with an outer class object.

If the inner classisstatic, then static inner class can be instantiated without an outer class instance, otherwise, the inner classobject must be associated with an instance of the outer class.

Inner class code has free access to all elements of the outer class object that contains it, by name (no matter what the access level of the elements is), if the inner class has a varible with same name then the outer class’s variable can be accesse like this:

.this.

The outer class can call even the private methods of the inner class.

Static Inner Classes:

Syntax for static inner class is as follows:

class OuterClassName {

public static class {

. . .

}

. . .

}

for static inner classesfollowing additional properties hold:

Static members of the outer class are visible to the static inner class, what ever their access level be.

Non-static members of the outer class are not available, because there is not instance of the outer class.

Aninner classmay not havestaticmembers unless the inner class is itself marked asstatic.

Sometimes static nested class are not refered to as inner class at all, as they don’t require outer classes instance.

A static inner class is just like any other inner class, but it dose not have the reference to its outer class object that generated it.

There are two more types of inner classes, i.elocal inner classes & anonymous inner classes. The local inner class are defined within a method. Anonymous inner classes are also defined with in a method but have no name.

Local Inner Classes:

Syntax of the local inner class is as follows:

class {

code...

(){

class {

code...

}

code...

}

code...

}

Local classes are never declared with an access specifier (that is, public or private). Their scope is always restricted to the block in which they are declared.

Local classes have a great advantage: they are completely hidden from the outside world.

They can not only access the instance variables but local variables of the method (in which they are defined) as well, but the local varible has to be declared final.

Anonymous Inner Classes

When using local inner classes, you can often go a step further. If you want to make only a single object of this class, you don’t even need to give the class a name.

Such a class is called an anonymous inner class. Usually the inner class extend some interface or extend other class.

This syntax for anonymous classes is very cryptic.

new SuperType(construction parameters) {

inner class methods and data

}

Here, SuperType can be an interface, such as ActionListener; then, the inner class implements that interface. Or SuperType can be a class; then, the inner class extends that class.

An anonymous inner class cannot have constructors because the name of a constructor must be the same as the name of a class, and the class has no name. Instead, the construction parameters are given to the superclass constructor. In particular, whenever an inner class implements an interface, it cannot have any construction parameters. Nevertheless, you must supply a set of parentheses as in

new InterfaceType () { methods and data }

It is recommended to refrain from using them as many programmers find too many anonymous classes hard to read.

The following table shows the types of nested classes:

Types of Nested Classes

Type

Scope

Inner

static nested class

member

no

inner [non-static] class

member

yes

local class

local

yes

anonymous class

only the point where it is defined

yes

An inner class may not have

staticmembers unless the inner class is itself marked as

static

--------------------------

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值