If no specifier is provided, variables are scoped to the default name space. As a result, they are accessible to the class itself, extended classes and other classes in the same package. This is why this scope is often referred to as package protected scope.

This this pointer is used so that the compiler can disambiguate two variables in the same scope with the same name. For example consider this method...


public void setValue(String value) {
     value = value;
}

Although this would be legal code, it would simply assign the method parameter value to itself. By qualifying the instance field 'value' with a this qualifier the compiler now knows to assign the parameter to the instance field.

public void setValue(String value) {
     this.value = value;
}


In short, the second method is probably what you want.

- JR


On 05/26/2015 10:19 AM, Danielle Livneh wrote:
Are variables by default public or private in java classes? Also I'm a bit confused why it would be necessary to use "this" since the methods (a getter method for example) have access to the variables anyway
--
You received this message because you are subscribed to the Google Groups "JPassion.com: Java Programming" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
Visit this group at http://groups.google.com/group/jpassion_java.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "JPassion.com: Java Programming" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
Visit this group at http://groups.google.com/group/jpassion_java.
For more options, visit https://groups.google.com/d/optout.

Reply via email to