Tips & Tricks

Here are some basic implementations of java language,
which you would like to know.

Have a look at the given program below. Found anything unusual?? You got it right! The program below runs without the main method. Here we have used the static block that executes the class directly as soon as it gets loaded without creating any object for it. At the run time, JVM looks for the main method to execute but fails to get it hence terminates the execution. However before exiting from the program it will throw an exception. So use System.exit(0) to terminate the program at the end of the static block itself.

public class JavaProgramWithoutMainMethod{
System.out.println(“This java program runs without the main
JavaProgramWithoutMainMethodThis java program runs
without the main methodC:\javac>

Know to read text from Standard I/O Console

Let’s see an easy way to use standard input console to read the user input through the standard I/O classes for reading text from a file or a keyboard.

In the example given below, the BufferedReader class has used its readLine() method. The BufferedReader class is the subclass of the FilterReader class. To input text from a character-input stream, the BufferedReader class uses read() and readLine() methods as shown in the example. The instance variable of the BufferedReader class reads a single line of text from the input stream as shown.

public class ReadIt{
public static void main(String[] args) throws IOException{
BufferedReader in = new BufferedReader(new
System.out.println(“Enter text : “);
String str = in.readLine();
System.out.println(“You entered a String : “);

Output: Enter a string


C:\JavaJazzup >java ReadIt
Enter text : Hello Amit
You entered a String : HelloAmit

Know to capture a screen shot

Hey this is very interesting, as we take a screenshot by pushing the print screen button on the keyboard, same way we can do it through java programming. For this we need to use few methods and APIs as shown in the program below. The methods and APIs, which have been used to accomplish this task, are:

createScreenCapture(): An image is created which is read and displayed at the display screen with the help of this method. This is the method of Robot class. The area is created by getDefaultToolkit().getScreenSize() method of the Toolkit class.

write(): This is the method of ImageIO class. The captured
image is stored in the created image buffer and the image file for the captured image is made with the help of this method.

The three arguments taken by this method are:

• First is the rendered image.
• Second is the file format.
• And last is the output file name.

ImageIO: This is the class of javax.imageio.* package. This
class is used to read an image or can be used to write an
The program below displays how to capture a screen and
make a “jpg” file. After the execution of this program a frame appears on the screen, which holds a command button labeled as “Capture Screen Shot”. An input dialog box comes up as the “Capture Screen Shot” button is pressed. This input dialog box asks the user to type a file name, which has to be created. Then a message dialog box gets opened with message“Screen captured successfully.”

import javax.swing.*;
import javax.imageio.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
public class Screenshot {
public static void main(String[] args) throws Exception {
Screenshot ss = new Screenshot();
public Screenshot(){
JFrame frame = new JFrame(“Screen Shot Frame.”);

August 2007 | Java Jazz Up | 33
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
31, 32, 33, 34, 35, 36, 37, 38, 39, 40,            Download PDF