Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7066013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:01:50+00:00 2026-05-28T05:01:50+00:00

How do i access some variables from another class? For example, i have 2

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:01:51+00:00Added an answer on May 28, 2026 at 5:01 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class 'Data' that uses a getter to access some array. If
I want to give access to protected properties to another class. I have been
I would like to define and initialize some variables in web.xml and the access
I need to access some files with fstream in my C++ app on Windows.
I'm having some trouble with the idea of accessing variables from other classes. I
I have a java threads related question. To take a very simple example, lets
I have a balloonGameViewController.h and another class I made called balloon.h I want to
I have an activity non visible, with some variables that must be accesed by
Having some issues accessing my param tags from within actionscript. Essentially I have the
If I try and access some random string in the URL of my rails

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.