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

2012/4/20

how to pass object as parameter and why?

sanashah220 sanashah220

2012/4/20

#
i want to know why we pass object as parameter in an other class and why we pass it? and can i call instance variable as global variable if i make it static?
danpost danpost

2012/4/20

#
sanashah220 wrote...
can i call instance variable as global variable if i make it static?
No. An instance variable is specific to an object. Each object that is created within a class will get a seperate instance variable to work with (example: if I have a class called 'Apple' and a boolean instance variable 'private boolean ripe'; 'ripe' can be true for one Apple class object and false for another). By making the variable a static one, it becomes a class variable (as opposed to an object variable). There is only one of that variable, no matter how many Apple objects are created. Obviously 'ripe' would be a very poor candidate for declaring static (if you wanted to track how many apples were created, however, 'applesCreated' would be a prime choice for 'static status' -- sorry, had to!).
sanashah220 wrote...
i want to know why we pass object as parameter in an other class and why we pass it?
You will realize, soon enough, the importance of parameters (and why we pass them). In a nutshell, the information that is passed would be information that the method might (or absolutely) need to complete a task within it. (example, let us say we wanted to create some apples; some ripe, and some, well. We can create a constructor for the Apple class to create the kind we want by allowing it to recieve the information that is required.
public static int applesCreated = 0;
private boolean ripe = false;

public Apple(boolean inRipe)
{
    ripe = inRipe;
    updateImage(); //method you create to get the proper image for apple based on the value of 'ripe'
    applesCreated++;
}

public Apple()
{
    this(Greenfoot.getRandomNumber(2) == 0); 
}
The 'this(Greenfoot.getRand...' statement calls the other constructor that takes a boolean parameter to create a random apple (that may or may not be ripe). Now you can call 'new Apple()' to create a random apple, or you can call 'new Apple(false)' to create an unripe apple, or you can call 'new Apple(true)' to create a ripe apple (red and delicious).
danpost danpost

2012/4/20

#
Let me qualify the static part. If the variable is in the world class, it is pretty much the same. Non-static variable, one per world instance; and static variable, one world class variable (I will not even say 'per', here; and I guess that would be considered 'global'). The values of non-static world variables are lost at 'reset', where static world variables retain their values until re-compiled. The reason I started with 'No' above is that you can never call an instance variable a global one; so, logically speaking, 'No' is correct.
You need to login to post a reply.