I am making a tic-tac-toe game and I am scanning in the players name and piece in the viewer. How am I able to access that in the component class? I am not able to put a method in my viewer, so I can't say getPlayer of getPiece, but there has to be some way to pass that variable into a different class. Thank you!

7 Years
Discussion Span
Last Post by santiagozky

We can help you if you don't provide more information. What viewer are you talking about? how does your classes relate? give us a little code or at least describe your application design so we can understand what you are trying to accomplish.

if what you wan is to pass a variable to another object you must create a "setter" method:

public void setVariable(Object object){

we are making a program with a JFrame, so the viewer class makes the JFrame, scans in all of the user input, and adds the component to the viewer. The component class puts all of the other classes together to make the actual game and to play. In the viewer class, we use a scanner to get the two players' name and what letter they want to be. Then, we need to use those variables in the component class, but we cannot make a getPlayer or getPiece method in the viewer class, so how can we access the variables from the Viewer class to be able to use them in the component class?? Thank you so much for your help!


Why cant you access the variables from the viewer? Are you declaring them as instance variables? You have to declare them outside every method so you can create a setter and a getter.

When are you creating your component object? you could pass the variables in the constructor when you create it.

Another way is to pass the component to the method that scan's for your data and set it at that moment:

public int scan(parameter1, parameter2, component){
//your stuff...
int x= yourVariable;
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.