Welcome to Java Q&A. In this new blog, I answer all kinds of technical questions related to Java. Each post presents one or more Java questions (based on a theme) and offers answers. Feel free to contribute your Java questions.
Could not find or load main class
Q: When I execute java classname.class
instead of java classname
, why do I observe the following error message in response?
Error: Could not find or load main class classname.class
A: classname.class
refers to a package hierarchy. The java
tool "thinks" that you are looking for a classfile named class
that is located in the classname
package. It cannot locate this classfile (which would have to contain a public static void main(String[] args)
method) and outputs this error message in response.
Declaring classes in interfaces
Q: Does Java allow classes to be declared in interfaces?
A: Java lets you declare classes in interfaces. Each declared class is implicitly public
and static
. Listing 1 presents a simple demonstration.
Listing 1. Declaring a class in an interface
interface Interface
{
int CONSTANT = 10;
class Class
{
static
{
System.out.println(CONSTANT);
}
}
}
Listing 1 reveals an interface named Interface
that declares an int
-based constant named CONSTANT
, which is implicitly public
, static
, and final
. It also declares a class named Class
, which is implicitly public
and static
. Nested classes can access interface constants, which is why Class
is able to access CONSTANT
(from its static
initializer).
It probably seems strange to declare a class within an interface, but this capability is useful when you want to tightly bind a class or enum to an interface to avoid unnecessarily cluttering the namespace. Listing 2 demonstrates this tight binding.
Listing 2. Tightly binding a class to an interface
public interface Addressable
{
class Address
{
private String boxNumber;
private String street;
private String city;
public Address(String boxNumber, String street, String city)
{
this.boxNumber = boxNumber;
this.street = street;
this.city = city;
}
public String getBoxNumber()
{
return boxNumber;
}
public String getStreet()
{
return street;
}
public String getCity()
{
return city;
}
@Override
public String toString()
{
return boxNumber+" - "+street+" - "+city;
}
}
Address getAddress();
}
Listing 2 declares an Addressable
interface that describes any entity associated with an address; for example, a letter, parcel, or postcard. This interface declares an Address
class to store the address components, and an Address getAddress()
method that returns the address.
Assuming the existence of Letter
, Parcel
, and Postcard
classes whose constructors take Address
arguments, the following code fragment shows you how you could construct an array of addressables and then iterate over it, obtaining and printing each address:
Addressable[] addressables =
{
new Letter(new Addressable.Address("10", "AnyStreet", "AnyTown")),
new Parcel(new Addressable.Address("20", "Doe Street", "NewTown")),
new Postcard(new Addressable.Address("30", "Ender Avenue", "AnyCity"))
};
for (Addressable addressable: addressables)
System.out.println(addressable.getAddress());
You'll probably find yourself more often declaring enums in an interface. For example, Listing 3 presents a Switchable
interface that nests a State
enum to describe a pair of switchable states.
Listing 3. Declaring an enum in an interface
public interface Switchable
{
enum State
{
ON, OFF
}
State getState();
void switchState();
}
Symbolic identifiers
Q: I would like to use a symbol (e.g., π) to name an identifier. How do I accomplish this task?
A: Java supports the use of various symbols (e.g., π) as identifier names. For example, Listing 4 presents a small Java application where π is the name of an identifier.
Listing 4. Symbolically naming an identifier
public class PI
{
public static void main(String[] args)
{
double π = 3.14159;
System.out.println(π);
}
}
Assuming a Windows platform and the Notepad editor, complete the following steps to save Listing 4 to a file named PI.java
:
- Run the Notepad editor.
- Copy and paste Listing 4 to Notepad's editor window. (To learn how to insert the PI symbol in a Microsoft Word, Open Office, or HTML document, check out eHow's How to Insert the PI Symbol article.)
- Select
Save As...
from theFile
menu to present theSave As
dialog box. - Change the dialog box's
Encoding
setting toUnicode
. - Enter
PI.java
into theFile name
text field. - Click the
Save
button to save the file and close the dialog box.
Launch a command window and change to the directory containing PI.java
. Because this source file contains Unicode characters, you must specify -encoding Unicode
when compiling this file, as follows:
javac -encoding Unicode PI.java
Assuming that a classfile is created, execute the following command line to run the application:
java PI
You should observe the following output:
3.14159
What's next?
Next time, I present several ways to customize Swing's file chooser component (e.g., a custom image viewer accessory). Before doing that, I review how to create and show the file chooser.
The following software was used to develop the post's code:
- 64-bit JDK 7u6
The post's code was tested on the following platform(s):
- JVM on 64-bit Windows 7 SP1
This story, "A new beginning" was originally published by JavaWorld.