Java FAQs

Contents

  1. Common error message: "java.lang.NoClassDefFoundError"
  2. Destructors in Java
  3. Are the argument and return value optional in "main"?
  4. Java in Cygwin
  5. Constructor quirk in Java?
  6. How can I provide default arguments in Java?
  7. What are system properties?
  8. What is the volatile modifier? (and synchronized modifier)
  9. What causes the compile error saying ''... is not abstract and does not override abstract method ...''?
  10. Tips on simplified exception handling?
  11. How to get size of an Applet?
  12. Why does AWT ignore my arguments?
  13. Error:non-static variable cannot be referenced from a static context


FAQs

  1. Compile error message: "java.lang.NoClassDefFoundError"
    Often this is because there is no main method in the class that you are executing. Or, the main method you define did not not have the correct signature.
  2. Destructors in Java
    There is no such because Java relies on Garbage collection. HOWEVER, the garbage collector only knows about Java resources (such as memory). It does not know about non-Java resources such as "opened files". For this, you need to implement your own functions called "finalizers". This is declared as
    	protected void finalize() throws Throwable {
    	...
    	}
    	
  3. Are the argument and return value optional in "main"?
    Answer is No. Only the first of the following four signatures are correct:
    	public static void main(String[] args)
    	public static void main()
    	public static int main(String[] args)
    	public static int main()
    	
  4. Java in Cygwin
    I am working in the Cygin environment on Windows, but my java is installed under the Window's directory. Can I access java from Cygwin?
            There is no difference between executable programs
            under cygwin and those "native" to Windows.  Suppose
            you installed java in some standard location
    	(say C:/"Program Files"/jdk1.4.1/bin) then
            you can directly invoke it.  Better still, just link to it:
    
                    > cd /usr/local/bin
                    > ln -s /cygdrive/c/"Program Files"/jdk1.4.1/bin/javac.exe .
                    > rehash
                    > javac myProg.java	-- now you can directly call javac
    
            If you need to access CLASSPATH, you can set this in the Control
            Panel, under Systems.  Alternatively, you can give this as
            an argument to javac.  E.g., you have a jar file in your cygwin
            directory /java/lib/pg73jdbc.jar, you can access it thus:
    
                    > javac -classpath C:/cygwin/java/lib/pg73jdbc.jar myProg.java
    
            Note that the path here begins from C:, and assumes
            your cygwin is found under C:/cygwin.
    	
  5. Constructor quirk in Java?
    Say X is a class. We want to define the constructors X() and X(a). I want X(a) to be the basic constructor and X() to call X(a). This seems impossible? Java forces you to make X() more BASIC than X(a). E.g.
    	class X {
    	  int val;
    
    	  X(int a) {val = a;}
    
    	  X() {X(1);}	// calls X(a)
    	
            }
    	
  6. How can I provide default arguments in Java?
    Java does not provide this ("safety feature"), but there are two basic ways to get around it. Here is the answer from http://www.jguru.com/faq/view.jsp?EID=331645.
    The first is to use method overloading where simpler versions of the method
    (i.e., those with fewer parameters) invoke the more complete version of the method with the default values appropriately specified in the argument list. The second is to identify special cases directly and then substitute the default values.
    
    Here's an example of the first from Scott Stanchfield:
    
        Basically, you define one method that has all of the parameters present. This method will perform all of the necessary processing.
    
        Then, you overload the method will some parameters missing. These overloaded methods call the one with all parameters by passing in default values.
    
        For example:
    
    public void foo(String name,
                    int age,
                    double income,
                    Color eyeColor) {
      this.name = name;
      this.age = age;
      this.income = income;
      this.eyeColor = eyeColor;
    }
    
    public void foo(String name,
                    int age,
                    double income) {
      foo(name, age, income, Color.blue);
    }
    
    public void foo(String name,
                    int age) {
      foo(name, age, 10000, Color.blue);
    }
    
    public void foo(String name) {
      foo(name, 20, 10000, Color.blue);
    }
    
    public void foo(String name, 
                    double income) {
      foo(name, 20, income, Color.blue);
    }
    
    Here's an example of the second approach:
    
    public void foo(String name,
                    int age,
                    double income,
                    Color eyeColor) {
      if (name == null)
        this.name = "John Doe";
      if (age <= 0)
        this.age = 20;
      if (income <= 0)
        this.income = 10000;
      if (Color == null)
        this.color = Color.blue;
    }
    	
  7. What are system properties?
    You can lookup and change various properties of your system.
    E.g., user home directory, java installation directory, etc.

    Here is a page from http://java.sun.com/docs/books/tutorial/essential/system/properties.html

  8. What is the volatile modifier? (and synchronized modifier)
    Here is an explanation from www.javaperformancetuning.com.
  9. What causes the compile error saying ''... is not abstract and does not override abstract method ...''?
    Say your compile error actually says ''MyList is not abstract and does not override abstract method retainAll(java.util.Collection) in java.util.Collection''. This means that you are defining a class ''MyList'' that implements the abstract class ''Collection'' (from java.util), and you have not provided an implementation of an abstract method called ''retainAll(..)'' in ''Collection''.

    Remark: Apparently you have a declaration of the form public class MyList implements Collection{...}. Since Collection is an abstract interface, the concrete class ''MyList'' must provide a implementation of every method in the ''Collection'' interface.

  10. Tips on Simplified Exception Handling?
    The simplest solution is to insert the following line of code whenever some error occurs:
    	errorFlag=false;
    	...
    	if (errorFlag)
    		throw new RuntimeException("My Error!");
    	
    There is no other code to write...
  11. How to get size of an Applet?
    A: Use getSize(). E.g.
    	 public void paint(Graphics g) {
    	
    	    Dimension appletSize = this.getSize();
    	    int mySize = appletSize.height;
    	    int myWidth = appletSize.width;
    	    
    	    g.drawString("This applet is " + myHeight + 
    	      " pixels high by " + myWidth + " pixels wide.", 
    	      15, appletHeight/2);
    	  }
          
  12. Why does AWT ignore my arguments?
    A: You must remember that AWT (since Java 1.1) defers to the look-and-feel of the platform (MacOS, Windows, etc). For instance, in MacOS, if you assign a color to a Button, this color information is ignored.
  13. Error: non-static variable cannot be referenced from a static context
    A: This is a standard error when your main method tried to access a non-static member of your class. The fix is to make your member into a static variable, or to construct an object of your class that has access to that member.


Please send comments to yap(at)cs.nyu.edu.