I\'m making a cookie clicker clone in java to practice my java skills and I have a small problem, I have variables that are declared in the main method that I want to access
Your variables should be fields.
Fields are declared outside of a class's methods and are usually found right below the class declaration. Fields can be accessed by all methods of a class.
They can also be accessed from other classes (unless they are private) using the dot operator.
static
, its class name is used to reference it.Example
public class Man {
public String name; //this is a field
public static String gender = "Male"; //this is a static field
public Man(String newName) {
name = newName; //assigns the value of a field from within a method
}
}
and another class...
public class Main {
public static void main(String[] args) {
Man bob = new Man("Bob");
System.out.println(bob.name); //referenced from object, prints Bob
System.out.println(Man.gender); //referenced from class name, prints Male
}
}
And to have more control over the access of your fields, you can use getters and setters. Take a read!
Using fields and their accessor methods. An example here.
Here, I will give you an example for exactly what you need. In this code you simply just need to set anything you would like to add to actionPerformed
as static
.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
public class testJava implements ActionListener {
protected static JButton b; // since this is static you can
// now access it in other classes
public static void main(String[] args) {
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource() == b) {
// do stuff here
}
}
}
public class ActionClass {
{
private static int clicks;
@Override
public void actionPerformed(ActionEvent e) {
clicks++;
}
public static void setClicks(int c){
clicks = c;
}
public static int getClicks(){
return clicks;
}
}
public class AnyClass {
{
// now you have access to your clicks count .
int clicks = ActionClass.getClicks();
// set value of clicks
ActionClass.setClicks(0);
}
You would have to make the variables public class variables instead of method variables, thereby increasing the scope and visiblity of the variables. Like so:
public class ActionClass {
{
public string MyPublicVariable = "woot";
@Override
public void actionPerformed(ActionEvent e) {
...
}
}
A more popular/recommended way to do this is to use a getter/setter instead of making the variable explicitly public. You would access a private variable through public methods like so:
public class ActionClass {
{
@Override
public void actionPerformed(ActionEvent e) {
private string MyPublicVariable = "woot";
public void setMyString(string newString){
MyPublicVariable = newString;
}
public string getMyString(){
return MyPublicVariable;
}
}
}
This way, you have more control over what your variables are set to.
You can pass main class instance reference to another class instance, or register callback. For the first way
Class MainClass {
private int mValue;
public void init() {
AnotherClass cla = new AnotherClass(this);
}
public void setValue(int value) {mValue = value;}
public int getValue(){return mValue;}
}
Class AnotherClass {
private MainClass mMain;
public AnotherClass(MainClass ref) {
mMain = ref;
}
public void controlValue() {
if (mMain != null) {
mMain.setValue(1);
mMain.getValue();
}
}
}
For the second way 1. declare an interface 2. implement this interface in main class 3. register this implementation to another class. 4. get and set value in another class.
public interface ClassListener {
public void setValue(int value);
public int getValue();
}
public class MainClass implements ClassListener{
private int mValue;
public void registerListener() {
AnotherClass cla = new AnotherClass();
cla.registerListener(this);
}
@Override
public void setValue(int value) {
// TODO Auto-generated method stub
mValue = value;
}
@Override
public int getValue() {
// TODO Auto-generated method stub
return mValue;
}
}
public class AnotherClass{
private ClassListener mListener;
public void registerListener(ClassListener listener) {
mListener = listener;
}
public void controlValue() {
if (mListener != null) {
int value = mListener.getValue();
mListener.setValue(++value);
}
}
}