Reputation: 2286
How do i access some variables from another class? For example, i have 2 files:
Main.java
package keyboardgame;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Main extends Applet implements KeyListener
{
private static final long serialVersionUID = 1L;
private static boolean keyboardRightPressed = false;
private static boolean keyboardLeftPressed = false;
private static boolean keyboardUpPressed = false;
private static boolean keyboardDownPressed = false;
int ballX = 20;
int ballY = 20;
int ballSpeed = 10;
public void init()
{
addKeyListener(this);
}
public void keyPressed(KeyEvent e)
{
int keyCode = e.getKeyCode();
if(keyCode == KeyEvent.VK_RIGHT)
{
keyboardRightPressed = true;
ballX = ballX + ballSpeed;
repaint();
}
if(keyCode == KeyEvent.VK_LEFT)
{
keyboardLeftPressed = true;
ballX = ballX - ballSpeed;
repaint();
}
if(keyCode == KeyEvent.VK_UP)
{
keyboardUpPressed = true;
ballY = ballY - ballSpeed;
repaint();
}
if(keyCode == KeyEvent.VK_DOWN)
{
keyboardDownPressed = true;
ballY = ballY + ballSpeed;
repaint();
}
}
public void keyReleased(KeyEvent e)
{
int keyCode = e.getKeyCode();
if(keyCode == KeyEvent.VK_RIGHT)
{
keyboardRightPressed = false;
repaint();
}
if(keyCode == KeyEvent.VK_LEFT)
{
keyboardLeftPressed = false;
repaint();
}
if(keyCode == KeyEvent.VK_UP)
{
keyboardUpPressed = false;
repaint();
}
if(keyCode == KeyEvent.VK_DOWN)
{
keyboardDownPressed = false;
repaint();
}
}
public void keyTyped(KeyEvent e) {
}
public void paint(Graphics g)
{
g.fillOval(ballX,ballY,20,20);
g.drawString("Right :"+keyboardRightPressed,0,10);
g.drawString("Left :"+keyboardLeftPressed,0,20);
g.drawString("Up :"+keyboardUpPressed,0,30);
g.drawString("Down :"+keyboardDownPressed,0,40);
}
}
KeyThread.java
package keyboardgame;
public class KeyThread implements Runnable
{
public KeyThread(String s,int speed)
{
name = s;
time = speed;
}
public void run()
{
try
{
if(keyboardRightPressed == true)
{
ballX = ballX + 10;
ballY = ballY + 10;
}
}
catch(Exception e)
{
}
}
}
What do i want is that from KeyThread.java to be able to modify/increment/access values from Main.java especially
int ballX = 20;
int ballY = 20;
so i can call my thread from Main.java like
Thread t5 = new Thread(new KeyThread("Moving Right", ballX = ballX + 1, 20));
t5.start();
How can i do that? And why is it so hard to do it?
Upvotes: 0
Views: 138
Reputation: 2405
Also, adding to the other answers, if a frequent task is going to be adding/subtracting to the value as it appears to be then perhaps a method such as adjustBallX(int x) would be valuable so you can increment/decrement the value without calling get() and set() every time.
Upvotes: 1
Reputation: 40356
If you declare your variables public
instead of private
, you can refer to them as (for example) Main.ballX
, but this is generally a bad practice. We organize things into classes to keep separate things separate; if we start using the members of class A within class B, then class B has a dependency on A that can make it hard to debug and maintain later on. Better would be to provide a "getter" or "accessor" method inside Main (eg getBallX()
and setBallX(int x)
) which give the owning class (in this case, Main) better control over its private members.
Upvotes: 1