0

I have a problem going on in Java.
I'm really new to Java, so don't blame me for weird code and stuff.

I'm making a small thingymabob that randomly generates numbers and uses them to create information about a randomly generated tree that is outlined by that information.
The problem is, I need to make a static void to print the stats of the tree.

But all my variables - "treeheight", "treetrunkwidth", etc. - can't be static, or every variable piece of information reverts to 0 or null. And this is annoying, because if I make my void for printing the tree stats not static, then it doesn't show up, but if I make it static, it won't let the tree's information be randomly generated and changed at will.

What do I do??

-AndeX

5
  • First of all you should post your code... then we will see what we can do... Commented Oct 8, 2013 at 3:16
  • You want some information to be associated with Object then instantiate the object and use. Commented Oct 8, 2013 at 3:16
  • You need to show some code. But it probably boils down to having new thingymabob() somewhere. Commented Oct 8, 2013 at 3:17
  • 1
    You can't because it should not. static should only able to access local variable and everything that declared static. Commented Oct 8, 2013 at 3:30
  • Why does this need to be static? There is one set of stats for each thingymabob, not one for the class. To print those make void printMe() or better yet override String toString(), which every object has. Then every thingymabob can describe itself with a string. Commented Oct 8, 2013 at 5:26

3 Answers 3

1

as simple as that

1 : static members can be accessed with class reference

ClassName.staticVar

2 : non static members can be accessed by instance reference

new CLassName()

now if you can please post your code , we can give exact suggestion

Sign up to request clarification or add additional context in comments.

2 Comments

I think Andex wants to call the instance variable by not using "new".
That doesn't make sense. Instance variables are exactly that, "instances" of the object. You can't do that.
0

I think what you are looking for is the Java Singleton Class. I recommend you look here for more information

It will mean that you will only ever have one instance of you class Thingymabob and you can access its properties as you normally would via getters and setters.

This is what the constructor and getInstance would look like:

private Thingymabob() {
    // Exists only to defeat instantiation.
}

public static Thingymabob getInstance() {
    if(instance == null) {
        instance = new Thingymabob();
    }
    return instance;
}

You would then access it in all your other classes like so:

Thingymabob thingy = Thingymabob.getInstance();

Comments

0

Inside Thingymabob you can create

public static void main(String[] argsIgnored) {
    Thingymabob anInstance = new Thingymabob();
    anInstance.doStuffToSetItUp()...
    System.out.println("treeheight = " + anInstance.getTreeheight ());
    System.out.println("treetrunkwidth = " + anInstance.getTreetrunkwidth ());
    etc...
}

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.