What is a Class:
- In object-oriented programming, a class is a construct that is used as a blueprint (or template) to create objects of that class. This blueprint describes the state and behavior that the objects of the class all share. An object of a given class is called an instance of the class. The class that contains (and was used to create) that instance can be considered as the typeof that object, e.g. an object instance of the "Fruit" class would be of the type "Fruit"
- A class is a representation for a conceptual grouping of similar terms. For example, a computer could be represented as a class which would have many subclasses such as personal computers, mainframes, workstations, etc.
- Java classes contain fields and methods.
- A class is simply a representation of a type of object; think of it as a blueprint that describes the object. Just as a single blueprint can be used to build multiple buildings, a class can be used to create multiple copies of an object.
Java Class Syntax:
Following is the syntax of Java class.
[public] class class-name [inheritance-specification] {
[field-list;]
[constructor-list;]
[method-list;]
}
Types of Java Class:
Java possesses five different types of class construct and if one is to develop quality software it is important to know where to use each type. It can sometimes be difficult to know exactly which variant should be used in a particular situation as often more than one will fit. To make matters worse there are generally many ways that a particular problem can be solved. This article will discuss the various class types and give indicators as to when they should be used.Top Level Classes
A top level class is the one that all Java developers will be familiar with as without it one can not develop software at all. A typical top level class can be seen below:package example;
public class Foo {
public static void main(String[] args) {
}
}This is the simplest, runnable, top level class that could be developed and lives in a file called Foo.java. Without actually checking I would guess that 90% or more classes are top level. Each class encapsulates some data or functionality that logically belongs on its own. If in doubt about which type of class to use a top level class is probably the safest choice as it will be easier to correct any mistakes later.
Static Member Classes
Static member classes (sometimes called static inner classes although they aren't strictly inner) are the first and most common type of nested class. A nested class is defined inside the body of an enclosing class and should only be present to assist the enclosing class. There is no limit to the depth of the class nesting but it is uncommon to see more than one level and very uncommon to see more than two (in more years than I care to think about developing Java I have never seen a triple nested class).
A very simple static member class is shown below. As with the top level class this is as simple as it can be and is just to show the syntax:
package example;
public class Nest {
private static class Nested {
}
}
public class Nest {
private static class Nested {
}
}
Static member classes can be thought of as regular classes that are simply defined inside another class. They have complete access to all the enclosing classes static member variables and functions. All the rules regarding accessibility of the member class are the same as for any other static member of the class. So, for instance, if it's private only the enclosing class can access it.
package example;
public class Nest {
private static String var = "foo";
private static class Nested {
public Nested() {
var = "bar";
}
}
}
public class Nest {
private static String var = "foo";
private static class Nested {
public Nested() {
var = "bar";
}
}
}
The most common use of static member classes is as an auxiliary class that only makes sense when used in conjunction with its enclosing class. An example of this is in the Java library is in he Map interface which includes the Map.Entry static interface. Although this example uses interfaces rather than classes the intention is exactly the same. Interfaces are used in order to make it easier to switch to a different implementation at a later date.
Non-Static Inner Classes
There is only one small difference between the declaration of a static and non-static member class but in terms of functionality the difference is huge. To define a non-static inner class use the same syntax as for static member classes but don't include the static key word. For Example:
package example;
public class Nest {
private String var = "foo";
private class Nested {
public Nested() {
var = "bar";
}
}
}
public class Nest {
private String var = "foo";
private class Nested {
public Nested() {
var = "bar";
}
}
}
The most important difference between static member classes and non-static inner classes can be seen in this example. Each instance of the inner class is implicitly associated with an instance of its enclosing class. This allows the inner class to directly access the non-static member variables and functions of its enclosing instance. Since a non-static inner class maintains a reference to its enclosing class an instance of a non-static inner class can not be created unless there is an instance of the enclosing class. Instances of the inner class maintain a reference to their enclosing instance which is assigned automatically upon object creation. If your member class doesn't require access to the enclosing instance make it static to save resources.
Non-static inner classes are useful as adaptors that allow a class to appear to be something else. For example one can view the keys in a Map as a Set. This is handled by the creation of an inner class that implements the Set interface which is then returned to the user. Iterators are a similar example. When one requests an Iterator from a collection what one receives is an inner class that implements the iterator interface. The class itself needs to be non-static so that it can access the contents of the enclosing instance.
Due to the restriction that a non-static inner class can't exist without an enclosing instance it is uncommon to see non-static inner classes that aren't private or at least package protected. Static member classes, in contrast, are frequently public.
Anonymous Classes
Anonymous classes are one of the strangest constructs in the Java language and many new developers have trouble getting their head around exactly what is happening. Once anonymous classes are properly understood, however, they open a world of possibilities for quickly solving simple routine problems. The most common use of anonymous classes is with GUI development where they are attached as listeners to the various widgets. Probably the second most common use of anonymous classes is as filters and comparators. The example below shows a simple comparator:
package example;
import java.util.Collections;import java.util.Comparator;import java.util.LinkedList;import java.util.List;
public class Comp {
public Comp() {
List<String> foo = new LinkedList<String>();
Collections.sort( foo, new Comparator<String>() {
public int compare( String s1, String s2 ) {
return s1.compareTo( s2 );
}
});
}
}
import java.util.Collections;import java.util.Comparator;import java.util.LinkedList;import java.util.List;
public class Comp {
public Comp() {
List<String> foo = new LinkedList<String>();
Collections.sort( foo, new Comparator<String>() {
public int compare( String s1, String s2 ) {
return s1.compareTo( s2 );
}
});
}
}
In the above example the anonymous class is an implementation of the Comparator interface that is used to order the List foo. The class is anonymous because it has no name and can't be referenced. In this situation the class is said to be used as a function object.
Anonymous classes can also be process objects such as Runnables or Threads and they can also be used in static factory methods where we want to view one object as if it is another by wrapping it in a class that implements a different interface.
Anonymous classes are a very powerful tool in the developers arsenal but it is important that they aren't over used or overly complex. A long anonymous class can quickly clutter a top level class making it difficult to see where one ends and the other begins. Generally speaking try and keep anonymous classes to a maximum of one screens worth of code or about 40 lines.
Local Classes
Local classes are by far the most uncommon class type to be used in the Java language. In fact I don't actually remember ever seeing one. For those interested an example is show below:
package example;
public class Enclosing {
public Enclosing() {
class Local {
public Local() {
System.out.println( "In local class...");
}
}
new Local();
}
}
public class Enclosing {
public Enclosing() {
class Local {
public Local() {
System.out.println( "In local class...");
}
}
new Local();
}
}
A local class is declared inside a method body (member classes are declared inside the class). It is only accessible to the method it is declared in which makes local classes less generally useful than non-static inner classes. If used in a non-static setting they acquire a reference to their enclosing class. There are some uses for local classes in situations where there isn't an interface already defined so an anonymous class would be difficult or impossible to use.