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

2017/6/27

Question about Kolling - Greenfoot, ch. 4, 4.5 and 4.6

HansR HansR

2017/6/27

#
Hi all, In chapter 4, 4.5, Kolling introduces variables, and he has "private GreenfootImage image1" in his code. Next he assigns it: image1 = new GreenfootImage ("crab.png"); Why is this split up in two stages? Why not at once: "private GreenfootImage crab.png" ? Thanks for the help, Hans
danpost danpost

2017/6/27

#
HansR wrote...
Why not at once: "private GreenfootImage crab.png" ?
What you meant here was -- why not:
private GreenfootImage image1 = new GreenfootImage("crab.png");
Why is this split up in two stages?
Probably because of what the example portrays. Oftentimes, beginners try to assign the variable with something like the following incorrect code as the second stage:
GreenfootImage image1 = new GreenfootImage("crab.png");
This does not set the new image to the field declared in stage one; but, instead creates a new variable and assigns the new image to it.
You need to login to post a reply.