Java FAQ

This FAQ is designed specifically for Lehigh students taking my graduate courses. More introductory information on Java can be found in Oracle's New to Java Center and Java Developer Tutorials and Training.

If you'd like to see a question and answer added, please send me an email.


Getting Started

  1. How do I obtain Java for use on my personal computer?
  2. How do I use it in Lehigh Public Sites?
  3. How can I setup my computer so that I can access Java from the command line?
  4. How do I edit my programs?
  5. How do I compile my programs?
  6. How do I run it?
  7. How can I setup my computer so that it knows where to look for Java class files?
  8. How can I setup my computer so that it knows where to look for JAR files?

Programming Questions

  1. Why am I getting a NullPointerException when I try to run my program?
  2. How do I find out what methods I can call on Strings?
  3. I am getting an Exception in thread "main" java.lang.NoSuchMethodError: main error, my code seems OK, what is wrong?
  4. How can I find out what class an object belongs to?
  5. Do I need to call super() in the constructor of a class that extends an abstract class?
  6. How do I print out different fields using a single System.out.println() command?
  7. How can an object pass itself as a parameter?

Getting Started

  1. How do I obtain Java for use on my personal computer?

    The set of Java tools that you will need to complete your assignment is referred to as SDK in Java terminology. For your personal use in your home computer you can download it from the download section of its homepage. Get the JDK of the most recent version by pressing the Download button. Windows users will get a self extracting utility. Note, you do not need to download Java EE nor NetBeans IDE.

  2. How do I use it in Lehigh Public Sites?

    Java is available through the standard public site software installation process. Simply, go to the Start menu, select "All Programs", and then select "Install Software" (it should be at the top of the menu). From the list of available software, select the Java JDK (not the runtime environment, that will not allow you to compile files), and proceed with the install instructions.

    Note, this will install Java in the "Program Files" directory. I don't know the exact path, but you should be able to find it. Note, the Java interpreter and compiler (java and javac) may not be in your system path.

  3. How can I setup my computer so that I can access Java from the command line?

    You need to add the directory where java.exe and javac.exe are to your system path. These files will be in a directory bin under your Java installation.

    Windows NT
    Go to Start -> Control Panel -> System. Select the Advanced tab, and press the Environment Variables button. If you already have a PATH variable (note, case does not matter), then edit it to add the full path to the Java bin directory. in variable value. If you don't have a PATH variable, or you are working on a Lehigh public machine, then press the New button under user variables, type in PATH for the variable name and type in the full path to the Java bin directory as the variable value.
    Windows 7
    Go to Start -> Computer. Select System Properties from the top menu and Advanced System Settings from the left panel. Press the Environment Variables... button and add the full path to the Java bin directory to either the System or User Path variables.

  4. How do I edit my programs?

    You can use any text editor to edit your programs. Below are some free Java editors that are available:

    If you are doing a complex project, then you may want to invest the time in learning an Integrated Development Environment (IDE). I am particularly fond of Eclipse. It takes a little time to figure out how to set it up, but once it is configured properly, it has some great features. In particular, you don't need to set any environment variables, it allows you to configure the JAR files and packages that you use on a project by project basis. If you do choose to use an IDE, then make sure that you can also compile and run your files using the standard javac and java commands. If I can't run them this way, then you won't get credit for the assignment!

  5. How do I compile my programs?

    Simply open an MS DOS Window, change to the directory where your files are stored, and type:

    javac MyProgram.java
    
    Note, this assumes that javac is in your system path. See the question on accessing Java from the command line above for how to set this.

  6. How do I run it?

    Use the java program. In the same MS DOS Window that you compiled the program, type:

    java MyProgram
    
    Note, type only the class name of the class you want to run. Do no type ".java" or ".class". Furthermore, this class must have a main() method or you'll get an error.

  7. How can I set up my computer so that it knows where to look for Java class files?

    By default, Java looks for class files in the current working directory. If you always run javac and java in the directory in which you create your source code files, then you shouldn't have to do anything.

    More complicated projects are often divided into packages. Packages have a hierarchical naming scheme. You should set up a directory structure that matches this naming scheme. Thus, if you have a package myapp.gui, then you should have a directory myapp/gui and all classes in the package should be placed in this directory. If you always compile and run programs from the directory which contains all of your package directories, then you shouldn't have to do anything special to work with these classes.

    However, sometimes you will want to use code developed for a different project, and may have packages scattered in different places in your system. In this case you need to set the CLASSPATH environment variable, which tells Java which directories to look for application and user classes in. This variable is set in the same way as the PATH environment variable. Note, if you have multiple entries in your CLASSPATH, they should be separated by semicolons (';'). It is good practice to include the current directory as the first element of the class path. This is done by placing '.' as the first path.

  8. How can I setup my computer so that it knows where to look for JAR files?

    JAR stands for Java Archive and is a way to exchange a set of Java class files as a single file. Java applications can use the class files in a JAR without having to unpackage the JAR first. To access class files in a JAR, simply include the JAR file in your CLASSPATH. Note, you must include the filename of the JAR file, not just the directory where it is stored.


Programming Questions

  1. Why am I getting a NullPointerException when I try to run my program?
  2. Most likely you tried to call a method on an object variable before initializing it. Remember, in Java all object variables are explicit heap-dynamic reference variables. As such, all object variables are null until you explicitly create and assign an object to them. Note, this is different from C++ where object variables are stack-dynamic by default. Consider the following Java code:

    MyObject x;       // x=null!
    x.myMethod();     // will result in a NullPointerException because x is null!
    ...
    MyObject y;
    y = new MyObject();  // y references a new object
    y.myMethod();        // this will work!
    

  3. How do I find out what methods I can call on Strings?
  4. As with all classes provided in the Java SDK, you can look it up in the online API documentation. In particular, here's the page for String.

  5. I am getting an Exception in thread "main" java.lang.NoSuchMethodError: main error, my code seems OK, what is wrong?

    Either you have forgotten to include a main() method in the class you tried to run or your main() method does not have the correct signature. The main() method should be static and should have an array of Strings as a parameter, e.g.,

    public static void main(String[] args) {
    ... 
    } 
    

  6. How can I find out what class an object belongs to?

    In order to test the type of an object, use the operator instanceof. The left operand must be an expression that results in an object (often just a variable), and the right operand must be the name of a class. This comparison evaluates to true if the object can be cast to the class (i.e., it is an instance of the class or an instance of a subclass of the class). Otherwise, it evaluates to false.

    Here's a short example using shapes:

    Shape s;
    ...
    /* if s is a Circle, then cast s to a variable of type Circle */
    if (s instanceof Circle) {
       Circle c = (Circle)s;
    }
    

  7. Do I need to call super() in the constructor of a class that extends an abstract class?

    No. Since abstract classes don't have constructors it would be a syntax error to call super(). Just implement the constructor of the subclass as if it does not have a parent class.

  8. How do I print out different fields using a single System.out.println() command?

    System.out.println() takes a single string. However you can use the "+" operator to concatenate different strings. Furthermore, variables are automatically converted to strings for printing, regardless of type. For example:

    int age = 21;
    System.out.print("Hello, I am " + age + " years old"); 
    
    prints out:
    Hello, I am 21 years old
    

  9. How can an object pass itself as a parameter?
  10. Simply use the reserved word this. For example, if an object wants to pass itself as a parameter to the bar() method of an object referenced by the variable foo, then it would make the call:

    foo.bar(this);