> After defining a object as a static variable can I change one of the object's variables?
> eg.
> public static Person p = new Person();
> later
> p.setName("Hendre");
> -- > You received this message because you are subscribed to the Google Groups "CTJUG Forum" group. > To post to this group, send email to CTJUG-Forum@googlegroups.com > To unsubscribe from this group, send email to CTJUG-Forum-unsubscribe@googlegroups.com > For more options, visit this group at http://groups.google.com/group/CTJUG-Forum > For the ctjug home page see http://www.ctjug.org.za > For jobs see http://jobs.gamatam.com/
You should be able to change the values inside the class - provided the 'p' variable is within static scope. You are applying your access modifiers only on the reference and not to the class.
> After defining a object as a static variable can I change one of the object's variables?
> eg.
> public static Person p = new Person();
> later
> p.setName("Hendre");
> -- > You received this message because you are subscribed to the Google Groups "CTJUG Forum" group. > To post to this group, send email to CTJUG-Forum@googlegroups.com > To unsubscribe from this group, send email to CTJUG-Forum-unsubscribe@googlegroups.com > For more options, visit this group at http://groups.google.com/group/CTJUG-Forum > For the ctjug home page see http://www.ctjug.org.za > For jobs see http://jobs.gamatam.com/
> After defining a object as a static variable can I change one of the > object's variables?
> eg.
> public static Person p = new Person();
> later
> p.setName("Hendre");
> -- > You received this message because you are subscribed to the Google Groups > "CTJUG Forum" group. > To post to this group, send email to CTJUG-Forum@googlegroups.com > To unsubscribe from this group, send email to > CTJUG-Forum-unsubscribe@googlegroups.com > For more options, visit this group at > http://groups.google.com/group/CTJUG-Forum > For the ctjug home page see http://www.ctjug.org.za > For jobs see http://jobs.gamatam.com/
Take care that you really need the variable to be static (see Ewald's reply). Thread safety can be an issue in a multi-threaded environment.
Regards Paul.
From: ctjug-forum@googlegroups.com [mailto:ctjug-forum@googlegroups.com] On Behalf Of Etienne Pretorius Sent: 09 February 2012 11:38 AM To: ctjug-forum@googlegroups.com Subject: Re: [CTJUG Forum] Static Variable
Hello Hendre,
You should be able to change the values inside the class - provided the 'p' variable is within static scope. You are applying your access modifiers only on the reference and not to the class.
Kind Regards, Etienne
On 09 Feb 2012, at 11:31 AM, Hendré Louw wrote:
After defining a object as a static variable can I change one of the object's variables?
eg.
public static Person p = new Person();
later
p.setName("Hendre");
-- You received this message because you are subscribed to the Google Groups "CTJUG Forum" group. To post to this group, send email to CTJUG-Forum@googlegroups.com<mailto:CTJUG-Forum@googlegroups.com> To unsubscribe from this group, send email to CTJUG-Forum-unsubscribe@googlegroups.com<mailto:CTJUG-Forum-unsubscribe@goo glegroups.com> For more options, visit this group at http://groups.google.com/group/CTJUG-Forum For the ctjug home page see http://www.ctjug.org.za<http://www.ctjug.org.za/> For jobs see http://jobs.gamatam.com/
-- You received this message because you are subscribed to the Google Groups "CTJUG Forum" group. To post to this group, send email to CTJUG-Forum@googlegroups.com<mailto:CTJUG-Forum@googlegroups.com> To unsubscribe from this group, send email to CTJUG-Forum-unsubscribe@googlegroups.com<mailto:CTJUG-Forum-unsubscribe@goo glegroups.com> For more options, visit this group at http://groups.google.com/group/CTJUG-Forum For the ctjug home page see http://www.ctjug.org.za For jobs see http://jobs.gamatam.com/
SANTAM Bpk / Ltd Reg. No. 1918/001680/06
Directors: VP Khanyile (Chairman), IM Kirk (Chief Executive), B Campbell, MD Dunn, MP Fandeso, BTPKM Gamedze, MLD Marole, JP Moller, YG Muthien, Dr J van Zyl, Y Ramiah, MJ Reyneke (Executive)
Company Secretary: M Allie
Please note: This email and its content are subject to the disclaimer as displayed at the following link http://www.santam.co.za/disclaimer.htm. Should you not have Web access, send a mail to disclai...@santam.co.za and a copy will be emailed to you.
> You should be able to change the values inside the class - provided the > 'p' variable is within static scope. You are applying your access modifiers > only on the reference and not to the class.
> Kind Regards, > Etienne
> On 09 Feb 2012, at 11:31 AM, Hendré Louw wrote:
> After defining a object as a static variable can I change one of the > object's variables?
> eg.
> public static Person p = new Person();
> later
> p.setName("Hendre");
> -- > You received this message because you are subscribed to the Google Groups > "CTJUG Forum" group. > To post to this group, send email to CTJUG-Forum@googlegroups.com > To unsubscribe from this group, send email to > CTJUG-Forum-unsubscribe@googlegroups.com > For more options, visit this group at > http://groups.google.com/group/CTJUG-Forum > For the ctjug home page see http://www.ctjug.org.za > For jobs see http://jobs.gamatam.com/
> -- > You received this message because you are subscribed to the Google Groups > "CTJUG Forum" group. > To post to this group, send email to CTJUG-Forum@googlegroups.com > To unsubscribe from this group, send email to > CTJUG-Forum-unsubscribe@googlegroups.com > For more options, visit this group at > http://groups.google.com/group/CTJUG-Forum > For the ctjug home page see http://www.ctjug.org.za > For jobs see http://jobs.gamatam.com/
class SomeClass { public static Person p = new Person();
}
You can also do:
SomeClass.p = null;
You might consider making p final. However, globally shared mutable state is generally a bad idea. For concurrency reaons, as Paul mentioned, but also by creating hidden dependencies between classes, which can, for example, make testing harder.
On Thu, Feb 9, 2012 at 1:50 PM, Moandji Ezana <mwa...@gmail.com> wrote: > Also note that in this case:
> class SomeClass { > public static Person p = new Person();
> }
> You can also do:
> SomeClass.p = null;
> You might consider making p final. However, globally shared mutable state > is generally a bad idea. For concurrency reaons, as Paul mentioned, but > also by creating hidden dependencies between classes, which can, for > example, make testing harder.
> Moandji
> -- > You received this message because you are subscribed to the Google Groups > "CTJUG Forum" group. > To post to this group, send email to CTJUG-Forum@googlegroups.com > To unsubscribe from this group, send email to > CTJUG-Forum-unsubscribe@googlegroups.com > For more options, visit this group at > http://groups.google.com/group/CTJUG-Forum > For the ctjug home page see http://www.ctjug.org.za > For jobs see http://jobs.gamatam.com/
> -- > You received this message because you are subscribed to the Google Groups > "CTJUG Forum" group. > To post to this group, send email to CTJUG-Forum@googlegroups.com > To unsubscribe from this group, send email to > CTJUG-Forum-unsubscribe@googlegroups.com > For more options, visit this group at > http://groups.google.com/group/CTJUG-Forum > For the ctjug home page see http://www.ctjug.org.za > For jobs see http://jobs.gamatam.com/
> > When I add final to the statement can I call p.setName("Hendre");
> Yes, because you are not making the instance final - you are making the > reference variable final.
> Kind Regards, > Etienne Pretorius
> -- > You received this message because you are subscribed to the Google Groups > "CTJUG Forum" group. > To post to this group, send email to CTJUG-Forum@googlegroups.com > To unsubscribe from this group, send email to > CTJUG-Forum-unsubscribe@googlegroups.com > For more options, visit this group at > http://groups.google.com/group/CTJUG-Forum > For the ctjug home page see http://www.ctjug.org.za > For jobs see http://jobs.gamatam.com/
> Fantastic! static and final works on the reference and not the instance.
> How do this relate to primitive types?
> Can i do the following
> public static int TEMP_VALUE = -1;
> then later do the following
> TEMP_VALUE = 2;
> or in the case of final
> public final static int TEMP_VALUE = -1;
> then later do the following
> TEMP_VALUE = 2;
> On Thu, Feb 9, 2012 at 2:36 PM, Etienne Pretorius <icewolfhun...@gmail.com > > wrote:
>> On 09 Feb 2012, at 2:33 PM, Hendré Louw wrote:
>> > When I add final to the statement can I call p.setName("Hendre");
>> Yes, because you are not making the instance final - you are making the >> reference variable final.
>> Kind Regards, >> Etienne Pretorius
>> -- >> You received this message because you are subscribed to the Google Groups >> "CTJUG Forum" group. >> To post to this group, send email to CTJUG-Forum@googlegroups.com >> To unsubscribe from this group, send email to >> CTJUG-Forum-unsubscribe@googlegroups.com >> For more options, visit this group at >> http://groups.google.com/group/CTJUG-Forum >> For the ctjug home page see http://www.ctjug.org.za >> For jobs see http://jobs.gamatam.com/
> -- > You received this message because you are subscribed to the Google Groups > "CTJUG Forum" group. > To post to this group, send email to CTJUG-Forum@googlegroups.com > To unsubscribe from this group, send email to > CTJUG-Forum-unsubscribe@googlegroups.com > For more options, visit this group at > http://groups.google.com/group/CTJUG-Forum > For the ctjug home page see http://www.ctjug.org.za > For jobs see http://jobs.gamatam.com/
> On Thu, Feb 9, 2012 at 2:36 PM, Etienne Pretorius <icewolfhun...@gmail.com> wrote:
> On 09 Feb 2012, at 2:33 PM, Hendré Louw wrote:
> > When I add final to the statement can I call p.setName("Hendre");
> Yes, because you are not making the instance final - you are making the reference variable final.
> Kind Regards, > Etienne Pretorius
> -- > You received this message because you are subscribed to the Google Groups "CTJUG Forum" group. > To post to this group, send email to CTJUG-Forum@googlegroups.com > To unsubscribe from this group, send email to CTJUG-Forum-unsubscribe@googlegroups.com > For more options, visit this group at http://groups.google.com/group/CTJUG-Forum > For the ctjug home page see http://www.ctjug.org.za > For jobs see http://jobs.gamatam.com/
> -- > You received this message because you are subscribed to the Google Groups "CTJUG Forum" group. > To post to this group, send email to CTJUG-Forum@googlegroups.com > To unsubscribe from this group, send email to CTJUG-Forum-unsubscribe@googlegroups.com > For more options, visit this group at http://groups.google.com/group/CTJUG-Forum > For the ctjug home page see http://www.ctjug.org.za > For jobs see http://jobs.gamatam.com/