• Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting variable from another class?  RSS feed

 
John Corkrend
Ranch Hand
Posts: 51
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So in my program there are two classes. One that is just a window with a text box, and the other that is a window where the game movement takes place. Now what I really want from this program is for the 'TextInput' class to have the variable 'clickCount'.

I have tried creating an instance of the OvalMovement class, but when I try to access the variable it says this on line 43 "Syntax error, insert "AssignmentOperator Expression" to complete Expression"
I don't know quite what to do here. And also how would I create an instance of a class without it creating the window all over again. For example if I take out line 43 in 'TestInput', the program will run but it will open a new window. I understand that it's just doing what it's told, creating an instance of the class, and that class is the window.






 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
test.clickCount is a variable and you can't just put the name of a variable in your code - it's meaningless. You need to do something with the variable, such as
Assign a value

or assign the value of the variable to another variable

or print out the value of the variable

etc, etc
 
Stevens Miller
Bartender
Posts: 1445
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John, at Line 43, you are accessing the clickCount member variable of the OvalMovement object referred to by "test," but you aren't doing anything with it. It's the same as if you had written "3" there. "test.clickCount" refers to the value stored in the public int clickCount in test's instance of an OvalMovement. To use it, you have to assign it to something (hence the error message you are getting), or pass it as a parameter. What you've written doesn't actually make anything happen. If it will help you understand what I mean replace Line 43 with this:
When you run your program, you'll see the value stored in test.clickCount printed on your console.
 
John Corkrend
Ranch Hand
Posts: 51
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well now there are on errors, but it still doesn't do what I want. It prints out the number 0 even if I've pushed a few buttons. Also it opens up a new window every time I type in "stats", is there a way to create an instance of the class without opening up a new window from that class every time.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12564
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Corkrend wrote:Well now there are on errors, but it still doesn't do what I want. It prints out the number 0 even if I've pushed a few buttons. Also it opens up a new window every time I type in "stats", is there a way to create an instance of the class without opening up a new window from that class every time.

yes, there is.

But nobody here can help you unless you TellTheDetails. Nobody here can guess what the problem is (at least, not with any accuracy), because we have NO IDEA what your code looks like.
 
John Corkrend
Ranch Hand
Posts: 51
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well my code is in my first post, I didn't feel like reposting that because I only made one change. But I can repost it if someone wants me to.
 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please repost the changed line and a few lines surrounding it, marking which is line 43.

There is something very wrong with a listener class with lots of if‑elses in. It looks very much like non‑object‑oriented design. And it has a confusing class name. you should have one Listener per action. You should also use a high‑level Listener. I think your text component may be able to use an ActionListener, but there is still something peculiar about writing HELP rather than using a help button.
 
Paul Clapham
Sheriff
Posts: 22832
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Corkrend wrote:Also it opens up a new window every time I type in "stats", is there a way to create an instance of the class without opening up a new window from that class every time.


Well, sure. Change the constructor of that class so that it doesn't open a new window. Put that new-window-creating code somewhere else. No, I don't know where else because my real answer to this question is "Why don't you just create one instance, which corresponds to one window, and use that instance every time you want to modify that window?"
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!