Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to declare Variables accessable from every method 1

Status
Not open for further replies.

aas1611

Programmer
Dec 14, 2001
184
DE
Hi,

I need to have a variable that can be accessed from every method in a class.

Here is what I did (the variable called IPAdresse):

Code:
public class FrageClient extends JFrame implements ActionListener {
   String IPAdresse;
   ...
   private void createAndShowGUI(int flag) {
      //this method can read the variable entered from main method
      System.out.println("IP: " + IPAdresse);
      ...
   }
   public FrageClient(String begin, String IPAd) {
	IPAdresse = IPAd;
	createAndShowGUI(0);
   }
	
   public static void main (String args[]) {
	new FrageClient("Start", args[0]); // args[0] is the IPAdresse
   }
   public void execute(String Answer) {
        // here is the problem -> IPAdresse comes out null
	System.out.println("IP: " + IPAdresse); 
   }

I run this java code with: java FrageClient 192.100.2.100

How to make my execute method read my IPAdresse?

Thanks for any clue!

Andre
 
By convention, method and class names start with non-capital letters.

And what happens between the call to createGUI and the call the execute? I guess there's something you're not telling us.

Cheers,
Dian
 
I got these method and class names from the book I'm studying from, so do not blame it on me :) But thanks for the info.

And you're right, I forgot to mention about the actionPerformed method:
Code:
public void actionPerformed(ActionEvent ae) {
   Object eventTarget = ae.getSource();
   if (eventTarget==Abschicken){
      strEingabe = Eingabe.getText().toString();
      FrageClient client = new FrageClient();
      client.execute(strEingabe);
   }
   ....
}

In the createAndShowGUI, I have a button called Abschicken, which means "submit", initiated like the following:

Abschicken = new JButton("Abschicken");
Abschicken.addActionListener(this);

So, does it give you a better insight to help me? :)

Andre
 
Sure.

1.- Throw that book away
2.- You're creating two instances of your FrageClient class: one in the main method and another one in the actionPerformed method. The first one receives the IP param, but not the second one.

Cheers,
Dian

 
Great! You are right, I have two FrageClient instances. Thanks for pointing it out.

While we're at it, I have an input field (an integer) in my GUI. How to make this input number disappear after I click the submit button? Something like refresh method, so the input field is empty again. For now, the number that I just entered stays there after I click submit button.

here is the code in createAndShowGUI:
Code:
Eingabe = new JTextField("");
...
workPanel.add(Eingabe);
...
pane.add(BorderLayout.CENTER, workPanel);

Andre
 
Add an actionListener to that submit button, and add some code to the actionPerformed method that clears the field value.

Cheers,
Dian
 
Alright, I'll give it a try. Thanks for you help but I won't throw my book away, though :)
Is it that bad having a method or class name with capital letter? It doesn't confuse me, and as long as it works, it's fine by me...

Andre
 
Yeah, it works, but it's hard to read by another people. A good program doesn't just work, it's reusable and maintainable.

That means other people can see your program, fix it or modify it in a easy way.

Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top