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

2012/4/29

dot notation

USBest USBest

2012/4/29

#
Hi! I've got a question about the dot notation in composition with 'this'. Is there a different effect, between:
public class Example extends Actor
{
    private int Number;

    public Example(int i)
    {
        Number=i;
    }
}
and
public class Example extends Actor
{
    private int Number;

    public Example(int i)
    {
        this.Number=i;
    }
}
SPower SPower

2012/4/29

#
No: you acces the same Number. In java, you don't need to put this for the methods or instance variables you want to use.
trash1000 trash1000

2012/4/29

#
It's a different story when you have local variables with the same name like one of your instance variables. Then you'd need to use 'this' when you want to use the instance variable.
SPower SPower

2012/4/29

#
An example:
private int number;
public AClass(int number)
{
      this.number = number;
}
Here is a class called AClass, it has an instance variable number. In it's constructor method is a parameter called number. Here we set the value of the instance variable to the parameter. It's better to don't do things like this, you may forget the "this." and then, your scenario doesn't work. Just give the parameters another name.
USBest USBest

2012/4/29

#
Does the computer need longer, when I write 'this.'?
SPower SPower

2012/4/29

#
No
Busch2207 Busch2207

2012/4/29

#
Well... I'm not sure, if you can say this so... Cause with the this the computer first has to recognize, that it's not the local variable... But I promisse, that you will never recognize it, if you write 'this.' in front of your variables ;)
danpost danpost

2012/4/30

#
I do not believe there would much, if any, difference in execution time between the two. This could stand correction, but it would make more sense that the added code would change load and compile times, as opposed to execution time. Although, it really may depend on how well the compiler creates the executable code (will both produce the same code, or does one of the two ways produce a round-about way to the same end). Either way, it would not be a noticable difference.
SPower SPower

2012/4/30

#
The compiler always needs to know where the variable comes from. If it's an instance variable, it will needs to check the instance variables. With "this." you do that yourself, so the compiler doesn't need to do so. They take the same time, and if there is a difference, you can't see the difference between the 2 ways: the difference is too small.
davmac davmac

2012/4/30

#
There is no difference, at all. The compiler produces the same bytecode regardless of whether you prefix access with 'this.' or not.
USBest USBest

2012/4/30

#
Ah ok! Thank you davmac! Then I assume from the fact that it also makes no difference how long the variable names are. :)
davmac davmac

2012/4/30

#
Yes. Furthermore, worrying about performance of different source constructs is, in general, pointless. In general you're not going to see a measurable performance difference without completely changing your algorithm.
You need to login to post a reply.