5

My overly long title says it all... I want to be able to access a variable from another class without creating a new object.

Currently the only way I know how to access another class's variable is:

Control control = new Control;

int dirtCount = control.dirtCount;

However, if I want to access this variable in my dirt object, I would have to create a new Control object for each one. This creates an endless cycle...

how can I access the variable without creating a new object?

(If you want to see the rest of my code, I can post it. I think that this part is the most relevant though :))

3
  • 2
    Read up on class variables and instance variables. Commented Mar 25, 2014 at 6:11
  • have you heard of static variables? Although I think that @SotiriosDelimanolis advice is good. Commented Mar 25, 2014 at 6:12
  • What about staic variable / class variable. Read Understanding Class Members. Commented Mar 25, 2014 at 6:14

3 Answers 3

17

One way would be declaring that variable as static, which means that it's a class variable (it's different than an instance variable). From Java Tutorial (emphasis mine):

They are associated with the class, rather than with any object. Every instance of the class shares a class variable, which is in one fixed location in memory. Any object can change the value of a class variable, but class variables can also be manipulated without creating an instance of the class.

In the Control class:

public class Control {
    public static int dirCount;
    // ...
}

and you can use it without creating an instance:

int dirCount = Control.dirCount;

Note:

If you want that variable to be private you can define a static getter method:

public static int getDirCount() {
    return dirCount;
}

and you can call that method with

int dirCount = Control.getDirCount();
Sign up to request clarification or add additional context in comments.

9 Comments

Thank you! I've used the static keyword before, but I had gotten "static" and "final" confused! I still have one issue: I made the variable static, and made a getter the same as yours, but when I tried to call it from my other class, I got this error message: "Cannot make a static reference to the non-static method getDirtCount() from the type Control". What should I do to correct this? Thank you again!
Since the method getDirCount will be static, you cannot use any non-static field or method inside. Make sure you only use static fields inside a static method. If you have dirCount and ` getDirCount()` identical to what I posted, there shouldn't be problem. Please check that you have the static keyword when declaring dirCount and getDirCount.
That isn't the problem... I can make the method getDirCount just fine, but the problem is that I cannot set a variable in the Dirt class to getDirCount in anything but the constructor for that class. I put this in the constructor
Ugh sorry I pressed enter. Anyway, I put this in the constructor: int dirtNumber2 = Control.getDirtCount(); this.dirtNumber = dirtNumber2;, and that worked, but I need to update the variable every step. I have a method that updates every step, but I get the error that I mentioned last time when I put what I have in the constructor in there. I have tried making the class static, but that didn't work because I don't want the variable in the Dirt class to be static. I guess what I need to do is convert the static variable into a non-static variable? Thank you again!
If it make sense (for your program) you can do everything static. But it depends on what is your class intended to do. An instance of Dirt won't never be created?
|
2

In java a class can have two type of member variables

1) instance variables - they are created with every object of that class, and can be access by object of that class.

2) class variables - they are belongs to class means each and every object can share same variable and can be access by class name

Member variables in java

Comments

0

Yes, you must read a little bit static variables. You can check it at http://www.caveofprogramming.com/frontpage/articles/java/java-for-beginners-static-variables-what-are-they/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.