Static class declarations
Tutorial Details:
Static class declarations
Static class declarations
By: By Java Q&A Experts
Can a class -- inner or outer -- be declared static?
Can a class (whether an inner or outer class) be declared static?
In order to understand the use of the static keyword in class declaration, we need to understand the class declaration itself. You can declare two kinds of classes: top-level classes and inner classes.
Top-level classes
You declare a top-level class at the top level as a member of a package. Each top-level class corresponds to its own java file sporting the same name as the class name.
A top-level class is by definition already top-level, so there is no point in declaring it static; it is an error to do so. The compiler will detect and report this error.
Inner classes
You define an inner class within a top-level class. Depending on how it is defined, an inner class can be one of the following four types:
1. Anonymous. Anonymous classes are declared and instantiated within the same statement. They do not have names, and they can be instantiated only once.
The following is an example of an anonymous class:
okButton.addActionListener( new ActionListener(){
public void actionPerformed(ActionEvent e){
dispose();
}
});
Because an anonymous class doesn't have a normal class declaration where it's possible to use static , it cannot be declared static.
2. Local. Local classes are the same as local variables, in the sense that they're created and used inside a block. Once you declare a class within a block, it can be instantiated as many times as you wish within that block. Like local variables, local classes aren't allowed to be declared public, protected, private, or static.
Here's a code example:
//some code block .......{
class ListListener implements ItemListener {
List list;
public ListListener(List l) {
list = l;
}
public void itemStateChanged(ItemEvent e) {
String s = l.getItemSelected();
doSomething(s);
}
}
List list1 = new List();
list list2 = new List();
list1.addItemListener(new ListListener(list1));
list2.addItemListener(new ListListener(list2));
}
3. Member. Member classes are defined within the body of a class. You can use member classes anywhere within the body of the containing class. You declare member classes when you want to use variables and methods of the containing class without explicit delegation.
The member class is the only class that you can declare static. When you declare a member class, you can instantiate that member class only within the context of an object of the outer class in which this member class is declared. If you want to remove this restriction, you declare the member class a static class.
When you declare a member class with a static modifier, it becomes a nested top-level class and can be used as a normal top-level class as explained above.
4. Nested top-level. A nested top-level class is a member classes with a static modifier. A nested top-level class is just like any other top-level class except that it is declared within another class or interface. Nested top-level classes are typically used as a convenient way to group related classes without creating a new package.
If your main class has a few smaller helper classes that can be used outside the class and make sense only with your main class, it's a good idea to make them nested top-level classes. To use the nested top-level class, write: TopLevelClass.NestedClass .
See the following example:
public class Filter {
Vector criteria = new Vector();
public addCriterion(Criterion c) {
criteria.addElement(c);
}
public boolean isTrue(Record rec) {
for(Enumeration e=criteria.elements();
e.hasMoreElements();) {
if(! ((Criterion)e.nextElement()).isTrue(rec))
return false;
}
return true;
}
public static class Criterion {
String colName, colValue;
public Criterion(Stirng name, String val) {
colName = name; colValue = val;
}
public boolean isTrue(Record rec) {
String data = rec.getData(colName);
if(data.equals(colValue)) return true;
return false;
}
}
}
And when you want to use it:
Filter f = new Filter();
f.addCriterion(new Filter.Criterion("SYMBOL", "SUNW"));
f.addCriterion(new Filter.Criterion("SIDE", "BUY"));
.....
if(f.isTrue(someRec)) //do some thing .....
One important note: The static keyword does not do to a class declaration what it does to a variable or a method declaration.
This page formated for crawlers and browsers that don't support scripts and tables.
Home
EZone
Read
Tutorial at: Click here to view the tutorial
Rate Tutorial: Static class declarations
View Tutorial: Static class declarations
Related
Tutorials:
Java decompilers
compared - JavaWorld - July
1997
Java decompilers
compared - JavaWorld - July
1997 |
Revolutionary RMI: Dynamic
class loading
and behavior
objects - JavaWorld - December 1998
Revolutionary RMI: Dynamic
class loading
and behavior
objects - JavaWorld - December 1998 |
Programming Java threads in the
real world, Part
7 - JavaWorld -
April 1999
Programming Java threads in the
real world, Part
7 - JavaWorld -
April 1999 |
Static class declarations
Static class declarations |
Create forward-compatible beans in EJB,
Part 2 - JavaWorld January
2000
Create forward-compatible beans in EJB,
Part 2 - JavaWorld January
2000 |
Build your own
languages with
JavaCC - JavaWorld December
2000
Build your own
languages with
JavaCC - JavaWorld December
2000 |
Printing in
Java, Part 4 - JavaWorld February
2001
Printing in
Java, Part 4 - JavaWorld February
2001 |
A primordial
interface? - JavaWorld March 2001
A primordial
interface? - JavaWorld March 2001 |
Language improvements
and models make
great Java - JavaWorld
Language improvements
and models make
great Java - JavaWorld |
Create a quick-and-dirty XML parser
Create a quick-and-dirty XML parser |
Java Tip 130: Do you
know your data
size?
Java Tip 130: Do you
know your data
size? |
roots of
constants classes
roots of
constants classes |
Simply
Singleton
Simply
Singleton |
Quite poor
testing
Quite poor
testing |
Nested Classes, Part 1
Nested Classes, Part 1
The concept of nesting a class within another class or method presents unique issues not found elsewhere in object-oriented programming. |
FindBugs, Part 1: Improve the quality of your code
FindBugs, Part 1: Improve the quality of your code
One of the problems with code quality tools is that they tend to overwhelm developers with problems that aren't really problems -- that is, false positives. When false positives occur, developers learn |
Understanding the Interplay Between Utility Classes and Static Initialization
Java is an OO language, which means much of the functionality of a Java application is encapsulated into cohesive classes that can be instantiated and acted upon. |
Introduction to JSP Declaratives Declarations
Introduction to JSP Declaratives Declarations
INTRODUCTION TO JSP DECLARATIVES
Syntax of JSP Declaratives are:
<%!
//java codes
%>
JSP Declaratives begins with <%! and ends %> with .We can embed any amount of java code in the JSP Declaratives. |
JSP FUNDAMENTALS
JSP FUNDAMENTALS
JSP FUNDAMENTALS
By: Hrishikesh Deshpande
Introduction :
JSP termed as Java Server Pages is a technology introduced by Sun Microsystems Inc. to develop the web application in more efficient way than Servlets. It has got many |
Introduction to JSP tags JSP Directives
Introduction to JSP tags JSP Directives
INTRODUCTION TO JSP TAGS
I n this lesson we will learn about the various tags available in JSP with suitable examples. In JSP tags can be devided into 4 different types. These are:
Directives
In the |
|
|
|