0

Lets say I have a class called Panel

 public class Panel extends JPanel implements ActionListener{
       public Board(){
            setFocusable(true);
            setBackground(Color.black)
            setDoubleBuffered(true);
            timer= new Timer(16,this);
            timer.start();
       }
       public void actionPerformed(ActionEvent e){
            //.........some codes here...//

       }
       public void render(Graphics2D g){
            //....some code here....//
       }
}

and I run it in the main class.

public class Main extends JFrame{
public Main(){
    add(new Panel());
    //....some code....//
}
public static void main(String[] args){
    new Main();
}
}

since actionPerformed() and render() methods are not in the constructor.

6
  • 3
    Try it and see what happens. Add a System.out.println(...) statement in each of the methods to see when they execute.
    – camickr
    Aug 21, 2013 at 4:36
  • my question is that render and actionPerformed are not in the constructor, how are they being executed? it is because of Timer?
    – Bango
    Aug 21, 2013 at 4:51
  • And that is what you should have originally stated in the question so we don't have to guess what you are thinking about. Anyway your question has been answered. So its time to accept an answer.
    – camickr
    Aug 21, 2013 at 4:54
  • sorry i had to pull an example out of my butt. and i did put actionPerformed() and render() methods are not in the constructor. so read before speak.
    – Bango
    Aug 21, 2013 at 4:59
  • You asked "what will happen". The implication is that you did not know what would happen. Read the answers. Nobody understood your real question until you clarified that the actionPerformed method was invoked. Then people looked closer at you code to explain the usage of the Timer.
    – camickr
    Aug 21, 2013 at 5:09

7 Answers 7

2

When you create a new object, only the constructor (and any methods it calls) will execute. In general, methods run only when called, either by your code or by the framework (typically in response to events—in your case, the event is the elapse of the Timer delay time).

Note that you had a typo in the code you originally posted:

public void actionPerfomred(ActionEvent e){

should be

public void actionPerformed(ActionEvent e){

If you annotate overridden methods with @Override, the compiler will catch errors like this for you. That way you won't have to figure out at run time why your method wasn't called.

12
  • The thing about programming is you need to think logically. So does it make sense that all the methods would automatically execute when a class is constructed? Spoon feeding doesn't help the OP to think for themselves. Learning some basic problem solving skills is something every programmer needs to learn.
    – camickr
    Aug 21, 2013 at 4:43
  • but actionPerformed method is called without being written in the constructor.
    – Bango
    Aug 21, 2013 at 4:44
  • @camickr - It does help to explain the basic rules of the road. Thinking logically only works if you have some facts to work with.
    – Ted Hopp
    Aug 21, 2013 at 4:44
  • @Bango - actionPerformed is called by the framework when an event (like a mouse click) occurs. Absent an action, the method won't be called.
    – Ted Hopp
    Aug 21, 2013 at 4:45
  • 1
    @Bango - The actionPerformed method alone is called by the timer. The render method is not automatically called then. However, the code in actionPerformed may trigger a call to render by the framework (if, for instance, you call repaint()) or it may call it directly.
    – Ted Hopp
    Aug 21, 2013 at 5:18
0

It will obviously call constructor only. If you don't have any constructor then compiler will call default empty constructor. But when you create an object only and only constructor is called.

0

Java will only run the constructor. Also i noticed that the constructor name of Panel class doesn't match with its constructor. It has to be same name. Instead of this

public class Panel extends JPanel implements ActionListener{
       public Board(){
            setFocusable(true);
            setBackground(Color.black)
            setDoubleBuffered(true);
            timer= new Timer(16,this);
            timer.start();
       }

}

you do

public class Panel extends JPanel implements ActionListener{
   public Panel(){
        setFocusable(true);
        setBackground(Color.black)
        setDoubleBuffered(true);
        timer= new Timer(16,this);
        timer.start();
   }
}

Also with regards to actionPerformed event methods, you have to manually wire them with the button/list on which you have to do some action.

0

It will call the constructor only .

is java going to run the constructor or the entire class?

No . Its not going to run the entire class .

Also while the class is loading it will run the stuffs inside the static block , if any are present .

0

but actionPerformed method is called without being written in the constructor.

Now that is a better question. That is the behaviour you should have stated when you first asked the question so we don't guess why you are asking the question.

That is because you are using a Timer. When the Timer fires the ActionListener added to the Timer is invoked. Your class implements the ActionListener so the actionPerformed() method of you class is invoked.

0
if i create a new object and put it in the main method, is java going to run
the constructor or the entire class?

First of all what do you mean by java going to run. When ever you call javac with the class names Eg javac *.java all of your java files will be compiled to an intermediate byte code. Now when you call java Main java interpreter will start interpreting your byte code starting from public static void main(String args[]) function. Java is just a name of programming language you can say is jvm going to execute the constructor or the entire class?

Secondly what do you mean by executing the entire class. There are two aspects in this

  1. Creating new object : Whenever you create a new object (by using the keyword new) corresponding constructor is invoked and the object is created. As the constructor is executed if there are any methods that you are calling from the constructor then they will get executed as well.
  2. Calling function/methods : If your methods are not private then you can call these methods after creating their object(depending on the access modifier used).

Your case

new Panel();

New Panel object is created(corresponding constructor is invoked).No other method will be executed on it's own.

my question is that render and actionPerformed are not in the constructor,
how are they being executed? it is because of Timer?

This is the question you should not be asking. You are the programmer and you must have written the code for your requirement. As for the use case I can tell you this

   public void actionPerformed(ActionEvent e){
        //.........some codes here...//

   }

you can create a JButton or for that matter any Component and add an ActionListner to it. An ActionListner has this method. Since you class Panel implements ActionListner you are implementing the function actionPerformed(). Eg.

    JButton button = new JButton();
    button.addActionListener(new Panel());
0

actionPerformed will call by timer trigger.

render method will not called. If you want paint something in swing JPanel, should overwrite paint(Graphics g) method defined in JComponent.

take a look at http://docs.oracle.com/javase/tutorial/uiswing/index.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.