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.
A: Java lets you declare classes in interfaces. Each declared class is implicitly public and static. Listing 1 presents a simple demonstration.
interface Interface
{
int CONSTANT = 10;
class Class
{
static
{
System.out.println(CONSTANT);
}
}
}
Listing 1: Declaring a class in an interface
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.
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: Tightly binding a class to an interface
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.
public interface Switchable
{
enum State
{
ON, OFF
}
State getState();
void switchState();
}
Listing 3: Declaring an enum in an interface
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.
public class PI
{
public static void main(String[] args)
{
double π = 3.14159;
System.out.println(π);
}
}
Listing 4: Symbolically naming an identifier
Assuming a Windows platform and the Notepad editor, complete the following steps to save Listing 4 to a file named PI.java:
Save As... from the File menu to present the Save As dialog box.
Encoding setting to Unicode.
PI.java into the File name textfield.
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
|
|
How much do you know about the Java language and platform? I've developed a 150-question quiz to help you assess your Java knowledge level. Below is an example question:
Which one of the following is not a Javadoc tag?
(a) This quiz is organized into interleaved true/false and multiple choice categories, and focuses on Java Platform, Standard Edition (SE) 1 through Java SE 7. The quiz is available for Amazon Kindle devices only. Check out Amazon's The BIG Java Quiz [3] to obtain a copy. |
Links:
[1] http://www.ehow.com/how_7643761_insert-pi-symbol.html
[2] http://tutortutor.ca/blogs/jqa/1/code.zip
[3] http://www.amazon.com/The-BIG-Java-Quiz-ebook/dp/B00EUTRDRA/ref=sr_1_1?ie=UTF8&qid=1377789568&sr=8-1&keywords=the big java quiz