• Post Reply Bookmark Topic Watch Topic
  • New Topic

Pass a variable from one class to another  RSS feed

 
Olof Sturesson
Greenhorn
Posts: 9
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As the title says, how can I pass a value from one variable to another class?

Example:

So here I'm switching one frame to another, called "redigeraProjekt". Now in this class, I want the value from .getSelectedIndex() pass over to that class. I've tried to use the variable "valIListan" but it cannot find it. Probably because it's "private" (?)

valIListan = listaAllaSpelProjekt.getSelectedIndex();
redigeraProjekt npj = new redigeraProjekt(); // "switching" to another frame
npj.setVisible(true);
this.setVisible(false);


I hope you understand what I'm saying

Thanks in advance!
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Olof Sturesson wrote:I hope you understand what I'm saying

Not really, because the code you've supplied won't compile as it stands.

What is the type of valIListan? Are you getting an error when you run your program? If so, what does it say?

However, in general terms (and if redigeraProjekt is a class that you wrote) you could possibly pass it in a constructor, viz something like:
int valIListan = listaAllaSpelProjekt.getSelectedIndex();
redigeraProjekt npj = new redigeraProjekt(valIListan);


but without more information, it's very difficult to know.

BTW: Class names should always start with a CAPITAL letter, so it should be RedigeraProjekt

Winston

 
Olof Sturesson
Greenhorn
Posts: 9
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even if you understood me or not, I made cause of you. Thanks. (passed it via a constructor, didn't work the first time for some strange reason)

Yeah, I'll use capital letters from now on, thanks for the tips
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Olof Sturesson wrote:Even if you understood me or not, I made cause of you. Thanks.

Great to hear. Glad we could be of help.

Winston
 
Randall Twede
Ranch Hand
Posts: 4696
8
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just some added comments. there are several ways to pass such information from one class to another. i am glad Winston's answer helped you. passing through a constructor is one way. you can also pass it as a parameter to a method of the second class. a third way is to have a public getter method in the first class that the second class can call(the second class will need a reference to the first class). a fourth way(not recommended), you can pass a reference to the first class to the second(either through a constructor or a method) and have your variable be not private(no modifier). then in your second class you can access it directly via FirstClass.variable
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!