Skip to content

Programming by Design

If you're not prepared to be wrong, you'll never come up with anything original. – Sir Ken Robinson

  • About
  • Java-PbD
  • C-PbD
  • ASM-PbD
  • Algorithms
  • Other

Java Boilerplate

Posted on December 25, 2017March 25, 2020 By William Jojo
Other

Write enough programs and you’ll soon discover that there seems to be a good deal of repetition in your programming. There seems to always be a main() method somewhere. Swing programs nearly always extend JFrame – or some other JThing. So why do we continue to write the same code over again?

Well, use a good integrated development environment (IDE) like Eclipse or Netbeans and you’ll discover that they’ve thought about this quite a bit as well. Sometimes these IDE’s include code we actually don’t want and hence we end up routinely taking something away – quite the opposite of what we described above.

Fortunately we can relax in knowing that sometimes we just need to have a clean starting point an then go from there. This document will illustrate some of the most common starting points and provide boilerplate.

For those who don’t know, boilerplate is a term for the rolled steel used to make, well, boilers. It’s the necessary foundation for building a boiler that won’t leak when filled with water, coolant or whatever liquid is used for heat transfer. The point being it is the fundamental foundation.


Complete Programs

So let’s take a look at a couple complete program boilerplate examples. The first is about the basic, Scanner-based, text-oriented interface – quite simple actually.

import java.util.Scanner;

public class MyClass {
  
  static Scanner kb = new Scanner(System.in);
  
  public static void main(String[] args) {
    
    // Your code here
    
  }
}

Example 1: Basic program boilerplate for a text-based interface.

Then we have a piece to represent the a Swing implementation.

import javax.swing.JFrame;

public class GUIClass extends JFrame {
  
  public GUIClass() {
    setTitle("My Title");
    setSize(300,300);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    
    // Your code here
    
    setVisible(true);
  }

  public static void main(String[] args) {
    
    GUIClass g = new GUIClass();
    
  }
}

Example 2: Boilerplate for a Swing GUI program.


Code Pieces

Some constructs are consistent as well. Consider reading input from the user or a file.

Scanner kb = new Scanner(System.in)
String s;

// ...

System.out.print("Prompt user: ");
while (kb.hasNext()) {
  s = kb.next();
  
  // do some work with s

  System.out.print("Prompt user: ");
}

Example 3: Sample EOF loop.

The code above works on the premise that the user will enter CTRL-D or CTRL-Z as needed or the end of the file causes hasNext() to return false.

So the process is:

  • Prompt
  • Check
  • Read
  • Work
  • Prompt
  • Repeat

Hopefully these little nuggets of code get you going and keep yo moving forward.

Good luck!

Post navigation

❮ Previous Post: What if we didn’t have Character?
Next Post: Facilitating Learning and Emerging Technology – Final Project – Spring 2018 ❯

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Copyright © 2018 – 2025 Programming by Design.