shruti tiwari via JavaKB.com coughed up:
This is my code and it works just fine....
in the first class:
public class home extends javax.swing.JFrame implements
ActionListener{
public static String temp5;
}
second class:
public class child extends javax.swing.JFrame implements
ActionListener{ home coobj=new home();
public child(){
System.out.println(coobj.temp5);
}
I'm sorry, I'm more than a little furious. I had over a page of
reformatting and explanations, and then my newsreader crashed. Let me cool
off before elaborating again.
In summary, here's the gist:
1. Always capitalize your class names or you'll confuse
the bejeebers out of other java guys reading your code.
Why this wasn't a fiat established by the language
spec, I'll never know.
2. 4 spaces for indents please. There are some that
argue with this, but most folks agree with 4 spaces.
Chances are great, however, that your indents got
screwed up simply by cutting and pasting into some
newsreaders.
3. In your example, you're creating a new Home object
every time the action listener is called. Given that,
you should have complete access to anything public
within it, /without/ the static:
public class Home (etc.) {
public String temp5;
}
elsewhere:
Home home = new Home();
System.out.println(home.temp5);
If you really wanted temp5 to be a static, then all you
needed to write was:
System.out.println(Home.temp5); // <-class name
4. If your code snippet is to grow in complexity, keep
in mind that there are likely to be multiple threads
accessing your object. There are too many ways
for this thing to grow, so I cannot comment on them
all, but keep in mind that you might just want to have
something of this form:
public class Home (etc.) {
private String temp5;
public String get() {
return temp5;
}
}
a. This keeps the temp5 from being settable by
others.
b. This gives you greater control later on as to when
specifically the string is retrieved. Something else
might be in the middles of constructing that string
when a method someplace tries to access it. Again,
just keep this in mind.
5. You never set your string to anything before using
it. I'll assume that you are doing so elsewhere.