How can I write an instance of a class in a static variable
Hi !
I have an instance of a class
Devisen dev = new Devisen();
In an other class I have a static method and I need there the content of some variables from dev.
public static void abc()
{ String text=dev.textfield.getText()
}
I get the errormessage, that the I cannot use the Not-static variable dev in a static variable.
I understand that I cannot reference to the class Devisen because Devisen is not static I so I had to reference to an instance. But an instance is the same as a class with static methodes. (I think so)
Is there a possibility, if I am in a static method, to call the content of a JTextField of an instance of a class ?
Thank you Wolfgang
My guess is that your code is something like this:
class A {
Devisen dev = new Devisen();
public static void abc() {
String text=dev.textfield.getText();
}
}
This makes dev an instansvariable, each instans of class A have its own copy of it. But abc is a static method and is therefore not associated with any instance and can not access instance variables.
If you post more of your code (the class that includes the variable dev and the method abc) I will be easier to help you. (use [code] and [/code] around the code to make it more readable)
You may be able to put a static function in Devisen to return the text in textfield.
public static String getTextFromField()
{
return textfield.getText();
}
And then you will be able to call this function from abc.
public static void abc()
{
String text = dev.getTextFromField();
}
This also might give you the same error message for the first function. I don't know. Give it a try.
Hallo, here is more code for my problem:
class Login {
Devisen dev=new Devisen();
}
class Devisen {
JTextField field2;
....
if (!Check.check_field2()) return; // if value not okay than return
...
}
class Check {
public static void check_field2()
{
HOW TO GET THE CONTENT OF field2 HERE ?
}
}
One solution ist to give the instance to the static function, with the keyword "this"
if (!Check.check_field2(this)) return;
and get the instance
public static void check_field2(Devisen dev)
BUT is that a problem for memory to give every method an instance of the class ? I have 50 fields to control and I dont want do give every check_method an instance of Devisen, if this is a problem for performance.
Or do I only give the place where the existing instance is.
Hmm...?
Thank you Wolfgang
not sure I fully understand (again), but why not use Observable/Observer? or events?
I read the manual now, because I dont know Observer. After reading I think I get an event from the Observer, but not the content of the object. But I need the content of a JTextField.Thank you for this idea.Wolfgang
pseudocode, why won't this work?
import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
class Devisen extends Observable implements ActionListener {
JTextField field2;
public JTextField getField2()
{
return field2;
}
private void initialize()
{
field2 = new JTextField();
field2.addActionListener( this );
}
public void actionPerformed( ActionEvent e )
{
notifyObservers();
}
}
public class Check implements Observer {
public Check()
{
Devisen dev = new Devisen();
dev.addObserver( this );
}
public void update( Observable obv, Object arg )
{
Devisen d = (Devisen) obv;
JTextField tf = d.getField2();
}
}
Yes it works. But I need no notification, I need only the content of a field if I ask for it. So I have found an easier way. I give the method an instance of the class without observation. Thank you for helping, I think I need observation for an other problem I have. I have not known this class. But you have helped me for an other problem. Thank you ! Wolfgang (KLD)