Anatomy of a Java program

516 views Asked by At

What is the terminology for each expression in the following class :

for e.g :

class Test {
    int a=0;
    void method(boolean boo){
        String b="";
        try
        {
            new Thread().sleep(1000);
        }
        catch(InterruptedException e){}
        JOptionPane.showMessageDialog(null,"test");
        BufferedImage image=ImageIO.read(new File("C:\\file.png"));
    }
}

From what i know a is a field, boo is a parameter, b and image are local variables.

What is the terminology used for

  1. new Thread().sleep()
  2. JOptionPane.showMessageDialog()
  3. ImageIO.read()
  4. new File()
  5. InterruptedException
3

There are 3 answers

0
Chetan Kinger On BEST ANSWER

new Thread().sleep() :

This is a two-part expression. The first part i.e, new Thread() is actually documented in the Java Language Specification (JLS) as an unqualified class instance creation expression :

Class instance creation expressions have two forms:

Unqualified class instance creation expressions begin with the keyword new.

An unqualified class instance creation expression may be used to create an instance of a class, regardless of whether the class is a top level (§7.6), member (§8.5, §9.5), local (§14.3), or anonymous class (§15.9.5).

You basically create an instance of the Thread class when you say new Thread(). The second part of the statement is .sleep() which is known as a method call.

new File() :

This is also an unqualified class instance creation expression just like the expression new Thread() but creates an instance of the File class instead of the Thread class.

JOptionPane.showMessageDialog() :

If you take a look at the source code for the JOptionPane.showMessageDialog method, you will see that the method is a static method. Also, the JLS explains how to access static methods :

A class method is always invoked without reference to a particular object.

What the JLS indirectly says is that you can access a static method outside the class in which it is defined using the name of the class.

Another important fact to understand here is that there are well defined Java naming conventions.

  1. Class names start with an upper case and any subsequent words are capitalized. So when you see any text of the form XyzAbc, assume that it is either a class or an interface. For example, JOptionPane and Image are class names.
  2. Similarly, there is also a convention in place for method names known as camel case. For example, whenever you see text such as doSomething, getSomething, setSomething, showMessageDialog().. you should know that it is a method.

Putting all this understanding together, we can deduce that JOptionPane.showMessageDialog() calls a static method from JOptionPane.

InterruptedException :

If you understood the naming conventions explained above, you should know by now that InterruptedException is a class. How it differs from any other class in Java is that it can be thrown around using the throw clause and then caught using a try-catch statement. You can read more about exception handling in the Oracle documentation.

0
nanofarad On

new Thread() is a constructor call. You are immediately calling sleep on the instance. This is odd, since sleep is a static method, so you can just do Thread.sleep(1000) for a static call (making sure to catch and handle InterruptedException.

JOptionPane.showMessageDialog() is a static method call as is ImageIO.read(). new File() is a constructor call that returns a File object.

Just a word on naming conventions: Field names (that aren't static final constants), variable names, and method names should be camelCase while class names should be PascalCase.

0
Jegg On

You might also need pay attention to the top of the program, you will find that String, Thread (you usually may not see those two),JOptionPane , ImageIO and BufferedImage are imported from somewhere.

For being programming better, you need follow the naming convention please look at this:http://www.oracle.com/technetwork/java/codeconventions-135099.html

Also please put modifiers in front of your class variables and methods