• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Referencing a public variable...

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there, I have a rather silly question, however its got the better of me!

This is simple example of the work I'm doing:

I have a variable:

public String varname;

and it stores some text.

How do i reference that variable in another form? For example, I have a label in a seperate form.

JLabel mylabel = new JLabel();

How do I get it to display the text from the variable (varname) in the other form

mylabel.setText(varname);

^^ This doesn't work. How do i reference the variable correctly?

Help much appreciated
Nick
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you say, "in a separate form," I'm assuming this means in a different class. If so, you'll need to reference an instance of the class containing the variable. For example...

mylabel.setText(myInstance.varname);

...where "myInstance" is an instance of the class in which varname is defined.

(Otherwise, please post the error message you're getting.)
 
Nick Davies
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply. Yeah sorry, i meant a seperate class not form.

The error it gives me is:



Any suggestions?

Nick
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It appears that the variable is actually within scope. The problem is that you're trying to access an instance variable from a static context (probably a main method).

Basically, the solution is the same: You need to use a reference to an instance that contains the variable.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could also declare the public variable to be static:

Of course if you do this, all instances of SomeClass.varName will be pointing to the same String object.
[ April 27, 2005: Message edited by: Ben Souther ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic