Question:
How does java distinguish this inside of objects?
2011-10-10 14:00:35 UTC
Here is a sample of code taken from a java game tutorial that explains double buffering. I understand the reason for the "this", but I don't understand how it works in this example. I'm thinking that dbImage must be null the first time around, and the measurements are set the second time around, but I'm not sure, because it doesn't seem right that "this" can be 'empty' as it were.

/** Update - Method, implements double buffering */
public void update (Graphics g)
{
// initialize buffer
if (dbImage == null)
{
dbImage = createImage (this.getSize().width, this.getSize().height);
dbg = dbImage.getGraphics ();
}

// clear screen in background
dbg.setColor (getBackground ());
dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);

// draw elements in background
dbg.setColor (getForeground());
paint (dbg);

// draw image on the screen
g.drawImage (dbImage, 0, 0, this);
}
Three answers:
deonejuan
2011-10-10 14:20:34 UTC
You have to understand a class file is text, or source code. When we invoke 'new', the constructor of that file runs and creates the instructions of the class file and makes an Object in RAM. Thus, we can have multiple copies of the class file and make many Objects.



So, within that code is the call to 'this', or current Object.
dukletha
2011-10-10 14:46:24 UTC
Most times, This in Java defines input given by the user, and can sometimes be input provided in the form of a file, what happens here is that the function is returning an object, dblmage, and takes the characteristics of it, and only creates when their is no initial input. Initially, it should be blank, or null as whenever you run the program you have to justify dbImage to the program before it can run a function on it. this is just system input or an object defined to the system by the user in this case.
schifano
2017-03-03 13:23:38 UTC
i'm no longer able to be troubled to look intimately at this utility yet you may %. to start authentic right here. class tuna{ public double upload(double num){ double sum; sum+=num; flow decrease back sum; } The variable sum is asserted every time you bypass into this equipment so gained't fairly deliver jointly as you %.. attempt class tuna{ private sum; public tuna() { sum = 0; } public double upload(double num){ sum+=num; flow decrease back sum; } have relaxing.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...