|
|
350 Mock Questions on SCJP 1.5 - JUST Rs.200 or 7 USD
Send us mail to sales@javabeat.net
more details
|
|
|
Package and class
declarations
|
A package represents a
group of classes. A package statement
should be the first valid statement in the source file. If there is no
package statement, the classes in the
source file belong to the default unnamed package, or else they belong to the
named package. Only one package
statement is allowed in a source file.
In a source file, there can be
only one public class, and the name of the file should match that of the
class. An import statement allows you
to use a class directly instead of referring to it using its fully qualified
name. Import statements must come after
any package statement and before the
class declarations, as shown below:
package com.abc; // package statement
import java.net.*; // wild card import (imports all classes in the package)
import java.util.Vector; // class import (only the given class is imported)
Nested class
declarations
A nested class or inner class is a class defined inside another
class. Nested classes can be non-static, method-local, anonymous, or
static.
Non-static inner
classes
A non-static inner class definition does not use the
static keyword (see
Java keywords
and identifiers for more information on keywords). It is defined within
the scope of the enclosing class, as follows:
class MyClass
{
class MyInner{}
}
To instantiate a non-static
inner class, you need an instance of the outer class.
A non-static inner class has
access to all the members of the outer class. From inside the outer instance
code, use the inner class name alone to instantiate it:
MyInner myInner = new MyInner();
From outside the outer instance
code, the inner class name must be included in the outer instance:
MyClass myClass = new MyClass();
MyClass.MyInner inner = myClass.new MyInner();
Modifiers that can be applied
to non-static inner classes are final,
abstract,
public,
private, and
protected.
Method local inner
classes
A method local inner class can be instantiated in the method where it is
defined. It can also be in a constructor, a local block, a static initializer,
or an instance initializer. It cannot access the local variables of the
enclosing method unless they are final.
The only modifiers for such a class are
abstract or
final.
Anonymous inner
classes
Anonymous inner classes are inner classes that have no name. They can extend a
class or implement an interface. For instance:
button1.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
button1_mouseClicked(e);
}
});
This example shows an anonymous
inner class. It is important to note that the class has been defined within a
method argument.
Static nested
classes
A static nested class is a static member of the enclosing class. It does not
have access to the instance variables and methods of the class. For
instance:
class MyOuter
{
static class MyNested{}
}
class Test
{
public static void main(String args[]){
MyOuter.MyNested n = new MyOuter.MyNested();
}
}
As this example shows, a static
nested class does not need an instance of the outer class for
instantiation.
|
|
350 Mock Questions on SCJP 1.5 - JUST Rs.200 or 7 USD
Send us mail to sales@javabeat.net
more details
|
|