This site requires JavaScript, please enable it in your browser!
Greenfoot back
Advenging
Advenging wrote ...

2011/7/11

GetX()

Advenging Advenging

2011/7/11

#
How can i used the getX of a class in an other class ? Thx for your answers. Advenging
GameCode GameCode

2011/7/11

#
You could use the following code: OtherClass o = new OtherClass(); o.getX(); or you could write into your other class: public static int x =0; public void act() { x = getX(); } and then you can use the static variable: OtherClass.x
Advenging Advenging

2011/7/12

#
Danke für deine Antwort Game code darauf wäre ich nie gekommen. Noch einen schönen Tag Advenging
mjrb4 mjrb4

2011/7/12

#
I'm not sure entirely what you mean, but if you've got an object of type actor (or subtype of actor) then you can call objectName.getX() without a problem (the getX() method is public meaning you can call it from anywhere else.) In terms of the above answer, is there a reason why x is static? :-)
GameCode GameCode

2011/7/12

#
@ mjbr4 If you don't want to do the OtherClass o = new Otherclass() stuff, you can use static variables, which every class can use. @Adwenging Ja, danke
mjrb4 mjrb4

2011/7/12

#
If you don't want to do the OtherClass o = new Otherclass() stuff, you can use static variables, which every class can use.
Nope, you're confusing public and static. Public means that any other class can use the variable, static means the variable belongs to the class and not instances of that class. So with static, every instance of the "other class" would always have the same value of x.
GameCode GameCode

2011/7/12

#
I think now you are confusing something. To see some examples for static, you could take a look at the API of the Greenfoot class. Though the Actor or World class don't extend Greenfoot they can use its methods because they're static 'public' doesn't mean that any other class can use the methods/variables. Still just Subclasses can use them.
davmac davmac

2011/7/12

#
There is a lot of confusion here from everyone and I think it starts with the question:
How can i used the getX of a class in an other class ?
The problem with the question is that getX() isn't a class method. Probably what was meant is: How can I use the getX method on another object? The answer is that you need a reference to the other object and then you say: otherActor.getX(); I while ago I wrote a fairly detailed example of this. Now: GameCode, you are not correct, it is "public" which allows other classes to access methods. You mention the API of the Greenfoot class - I'd like to point out that the methods that you can call in the Greenfoot class are not just static but also public. That is why you can call them. The "static" keyword just differentiates between class methods/variables and object methods/variables. With a static method, you can call it as "ClassName.methodName()", whereas for an object method, you must have a reference to the object you want to call the method on ("otherObject.methodName()", where "otherObject" is a variable). As a counterexample: the getHeight() and getWidth() methods in the World class are not static, but you can still call them from an Actor subclass (because they are public!).
GameCode GameCode

2011/7/12

#
As a counterexample: the getHeight() and getWidth() methods in the World class are not static, but you can still call them from an Actor subclass (because they are public!).
Yes, but you can only use them with getWorld(). ... When a method is public static you can use it without getWorld()
GameCode GameCode

2011/7/12

#
However, what I wrote above:
You could use the following code: OtherClass o = new OtherClass(); o.getX(); or you could write into your other class: public static int x =0; public void act() { x = getX(); } and then you can use the static variable: OtherClass.x
actually works. I used both ways in some of my scenarios!
davmac davmac

2011/7/12

#
When a method is public static you can use it without getWorld()
Yes; but you need to give the class name. So, you access a static method method in a different way. But it still needs to be public, and it is the "public" which allows the access, not the "static". (If the method was not static, you could still access it, via "new Greenfoot().methodName()" for example. If the method was not public, however, you couldn't access it at all). The problem with making variables static is that then there is only one variable for the class. Even if you have lots of objects, they all share the same value. Sometimes this is ok, but usually it's not what you want.
davmac davmac

2011/7/12

#
And: you can access non-public methods / variables from another class if they are in the same "package", as long as they aren't declared private. This is a Java idiom which isn't really represented in Greenfoot; all your classes are in the same package. But you can't call non-public methods from a class in a different package. For instance, there are some methods in the Actor class which are not public and which you can't call. (They're not listed in the API documentation - because you are not meant to be able to call them). Using 'static' works if there is only one object you are interested in. Even then it is best avoided if possible. For one thing, static variables are not automatically reset when the "reset" button is pressed.
You need to login to post a reply.