• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

init() not entered first

Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please look at the following code. It looks like a fairly simple applet but has some behavior that I find strange. As far as i know you should normally be able to make use of an initializer OR initialize a declared field in the init() method. The thing with the applet below though is that when I initialize Insets theInsets within the init() method, the applet won't run. It will when I make use of an initializer. The reason it doesn't run in the first case is because the getInsets method is looked at first and then it can't return theInsets since the value is still null (un-initialized). You can see this happen when you run it and look at the output,since I put some System.out.println lines inthere. More amazingly the getInsets method is even looked at twice before entering the init() method.
I wonder if there is someone that can explain this behavior...It sure is much appreciated !
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class MyInsetsButton extends JApplet
JButton theButton;
Container theContainer;
//Insets theInsets ;
Insets theInsets = new Insets(1,1,1,1);
public void init()
System.out.println("inside init()");
//theInsets = new Insets(1,1,1,1);
theButton = new JButton("Press Me");
theButton.addActionListener(new Switcher());
theContainer = this.getContentPane();
public Insets getInsets()
System.out.println("inside getInsets");
return theInsets;
public class Switcher implements ActionListener
public void actionPerformed(ActionEvent ae)
System.out.println("inside Switcher");
theInsets = new Insets(theInsets.top + 2, theInsets.left + 2,
theInsets.bottom + 2, theInsets.right + 2);
    Bookmark Topic Watch Topic
  • New Topic