aspose file tools*
The moose likes Beginning Java and the fly likes How does one control the order in which methods are executed? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How does one control the order in which methods are executed?" Watch "How does one control the order in which methods are executed?" New topic
Author

How does one control the order in which methods are executed?

Jerry Goldsmith
Ranch Hand

Joined: Nov 29, 2006
Posts: 53
I am relatively new to Java and am trying to understand the basics of program flow control within the language. I've experimented with action listeners, if statements, try catch finally blocks, inner classes and etc.

I would like to know how to get one method to execute after another. In old fashioned Basic and Fortan type languages, if Statement A appears after Statement B in the code, Statement A is executed first. This does not appear to be the case with Java.

Any pointers would be greatly appreciated.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


I would like to know how to get one method to execute after another. In old fashioned Basic and Fortan type languages, if Statement A appears after Statement B in the code, Statement A is executed first. This does not appear to be the case with Java.

Uhm...(assuming you mean "if Statement A appears after Statement B in the code, Statement B is executed first") it is the case with Java too. What makes you think otherwise? Can you give an example of the code that is confusing you?
[ January 22, 2007: Message edited by: Paul Sturrock ]

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi Jerry,

That's definitely the case in Java, within a single thread, anyway; it's just that you may not be looking at all the relevant code. For example, it might seem that your ActionListener is invoked in some magical way, but ultimately, there's a loop buried deep in Java's guts that's saying something like



In other words, there's no magic -- it's just that your code is part of a larger body of code you may not be immediately aware of. Same with inner classes, try blocks, etc. The flow of control is perfectly well-defined: there's no magic; you just have to look at it one step at a time. Nothing really happens unless you set it up to happen; for example, you can write a hundred inner classes, but none of their code will execute unless you somehow create instances of them and invoke their methods, or hand them off to other code that will do so.

If you write a method C, then you can make A happen before B within that method just by writing

A();
B();

nothing magic there, either!

Now, the specifics of what executes when do differ from applications to applets to servlets, etc, but again, that's just because there's other code involved that you didn't write yourself. For example, you might know that when you write an applet, you provide an init() method, a start() method, a stop method, etc. This might seem weird and magical until you realize that there is some code in the browse that says something like



[Jess in Action][AskingGoodQuestions]
Jerry Goldsmith
Ranch Hand

Joined: Nov 29, 2006
Posts: 53
Thank you for the replies to my noob question.

To be more specific, I have a Main class with a main method which contains methods get_name and show_name.

Method get_name creates a new instance of class getInput and executes method myGUI which prompts the user to input a part name and part description into text fields. When the OK button is hit, an action listener sets string_name to the part name.

Method setString then sets return_string equal to string_name which is returned to method get_name in the Main class, which sets got_name equal to return_string.

Method show_name should then be executed, displaying a frame which shows the string got_name. The trouble is, even though show_name appears after got_name in the main method, they both seem to execute simultaneously.

I'm attempting to develop a GUI class which can be executed from several different classes and pass strings to those classes. See below for the code:

Here is the Main class:

package javaapplication11;

import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;

public class Main
{
public static String got_name = null;

public static void main(String[] args)
{
get_name();
show_name();
}

public static void get_name(){
getInput MainInput = new getInput();
got_name = MainInput.myGUI();
}

public static void show_name(){
JFrame parentFrame = new JFrame();
JOptionPane.showMessageDialog(parentFrame, "got name is: "+got_name);
}

}

Here is the getInput class which is called from class Main:

package javaapplication11;

import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;

public class getInput implements ActionListener

{
public String string_name = null;
public String string_desc = null;
public String return_name = "test";
public static String return_desc = null;
public static JTextField text_name;
public static JTextField text_desc;
public static ActionListener new_listener;
public static JFrame parentFrame;

public String myGUI()
{
parentFrame = new JFrame("New Part");
text_name = new JTextField(20);
text_desc = new JTextField(20);
JLabel label_name = new JLabel("Part number: ");
JLabel label_desc = new JLabel("Part description: ");
JButton ok_button = new JButton("OK");
JPanel panel_1 = new JPanel();

panel_1.add(label_name);
panel_1.add(text_name);
panel_1.add(label_desc);
panel_1.add(text_desc);
panel_1.add(ok_button);

parentFrame.getContentPane().add(panel_1);
parentFrame.setBounds(400, 200, 250, 160);
parentFrame.setVisible(true);
ok_button.addActionListener(new getInput());
return return_name;
}

public void setString(){
return_name = string_name;
}

public void actionPerformed(ActionEvent e)
{
string_name = text_name.getText();
string_desc = text_desc.getText();
parentFrame.dispose();
JFrame parentFrame_2 = new JFrame();
JOptionPane.showMessageDialog(parentFrame_2, "Name: "+string_name+
"\nDescription: "+string_desc);
setString();
}
}
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
You might wind up with something more like ...

That's pretty sketchy ... does it hang together for you? We create a new GUI and tell it we're interested in whatever information it reads. When the GUI gets data, it's actionEvent notifies all listeners about the data. You can reuse the same input gui with any new listener, or even two listeners at the same time.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How does one control the order in which methods are executed?