Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Anonymous code blocks in Java

Tags:

java

Are there any practical uses of anonymous code blocks in Java?

public static void main(String[] args) {     // in     {         // out     } } 

Please note that this is not about named blocks, i.e.

name: {       if ( /* something */ )           break name; } 

.

like image 847
Robert Munteanu Avatar asked Oct 13 '09 21:10

Robert Munteanu


People also ask

What are code blocks in Java?

A code block is a grouping of two or more statements. This is done by enclosing the statements between opening and closing curly braces. Once a block of code has been created, it becomes a logical unit that can be used any place that a single statement can.

What is an anonymous object in Java?

Anonymous object. Anonymous simply means nameless. An object which has no reference is known as an anonymous object. It can be used at the time of object creation only. If you have to use an object only once, an anonymous object is a good approach.

How do you make an anonymous class in Java?

Object = new Example() { public void display() { System. out. println("Anonymous class overrides the method display()."); } }; Here, an object of the anonymous class is created dynamically when we need to override the display() method.

What is used in Java to surround code blocks?

Java try block is used to enclose the code that might throw an exception. It must be used within the method. If an exception occurs at the particular statement in the try block, the rest of the block code will not execute. So, it is recommended not to keep the code in try block that will not throw an exception.


2 Answers

They restrict variable scope.

public void foo() {     {         int i = 10;     }     System.out.println(i); // Won't compile. } 

In practice, though, if you find yourself using such a code block that's probably a sign that you want to refactor that block out to a method.

like image 179
David Seiler Avatar answered Sep 20 '22 23:09

David Seiler


@David Seiler's answer is right, but I would contend that code blocks are very useful and should be used frequently and don't necessarily indicate the need to factor out into a method. I find they are particularly useful for constructing Swing Component trees, e.g:

JPanel mainPanel = new JPanel(new BorderLayout()); {     JLabel centerLabel = new JLabel();     centerLabel.setText("Hello World");     mainPanel.add(centerLabel, BorderLayout.CENTER); } {     JPanel southPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0,0));     {         JLabel label1 = new JLabel();         label1.setText("Hello");         southPanel.add(label1);     }     {         JLabel label2 = new JLabel();         label2.setText("World");         southPanel.add(label2);     }     mainPanel.add(southPanel, BorderLayout.SOUTH); } 

Not only do the code blocks limit the scope of variables as tightly as possible (which is always good, especially when dealing with mutable state and non-final variables), but they also illustrate the component hierarchy much in the way as XML / HTML making the code easier to read, write and maintain.

My issue with factoring out each component instantiation into a method is that

  1. The method will only be used once yet exposed to a wider audience, even if it is a private instance method.
  2. It's harder to read, imagining a deeper more complex component tree, you'd have to drill down to find the code you're interested, and then loose visual context.

In this Swing example, I find that when complexity really does grow beyond manageability it indicates that it's time to factor out a branch of the tree into a new class rather than a bunch of small methods.

like image 45
Stephen Swensen Avatar answered Sep 19 '22 23:09

Stephen Swensen