Static Variable

34 views
Skip to first unread message

Hendré Louw

unread,
Feb 9, 2012, 4:31:23 AM2/9/12
to ctjug...@googlegroups.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");

Len Weincier

unread,
Feb 9, 2012, 4:37:32 AM2/9/12
to ctjug...@googlegroups.com
Hi

Yes you can.

The more complete example is : 

class SomeClass {
public static Person p = new Person();

}

Then somewhere else you can say : 

SomeClass.p.setName("Hendre");

hth
Len


--
You received this message because you are subscribed to the Google Groups "CTJUG Forum" group.
To post to this group, send email to CTJUG...@googlegroups.com
To unsubscribe from this group, send email to CTJUG-Forum...@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/

Etienne Pretorius

unread,
Feb 9, 2012, 4:37:36 AM2/9/12
to ctjug...@googlegroups.com
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:

Ewald Horn

unread,
Feb 9, 2012, 4:39:40 AM2/9/12
to ctjug...@googlegroups.com
Hi.

Yes, the static is not the same as in C++ - it simply means that the SAME variable will be shared across instances of the class.

It can still be modified, just like a normal instance variable.

Best regards,
Ewald

Paul Gilowey (Santam)

unread,
Feb 9, 2012, 4:43:55 AM2/9/12
to ctjug...@googlegroups.com

Hey Hendre,

 

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.

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 discl...@santam.co.za
and a copy will be emailed to you.

Hendré Louw

unread,
Feb 9, 2012, 4:44:25 AM2/9/12
to ctjug...@googlegroups.com
Thank you this answers my question.

Moandji Ezana

unread,
Feb 9, 2012, 6:50:45 AM2/9/12
to ctjug...@googlegroups.com
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

Hendré Louw

unread,
Feb 9, 2012, 7:07:13 AM2/9/12
to ctjug...@googlegroups.com
Can i also say

SomeClass.p = new Person();

--

Len Weincier

unread,
Feb 9, 2012, 7:23:34 AM2/9/12
to ctjug...@googlegroups.com

On 09 Feb 2012, at 2:07 PM, Hendré Louw wrote:

> Can i also say
>
> SomeClass.p = new Person();


Yes

Len

Munaf Sheikh

unread,
Feb 9, 2012, 7:25:54 AM2/9/12
to ctjug...@googlegroups.com

Can i also say

SomeClass.p = new Person();

yes. you can do anything you want to it. anything.


Hendré Louw

unread,
Feb 9, 2012, 7:33:56 AM2/9/12
to ctjug...@googlegroups.com
When I add final to the statement can I call p.setName("Hendre");

public static final Person p = new Person();


Etienne Pretorius

unread,
Feb 9, 2012, 7:36:00 AM2/9/12
to ctjug...@googlegroups.com

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

Hendré Louw

unread,
Feb 9, 2012, 7:44:40 AM2/9/12
to ctjug...@googlegroups.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;


Munaf Sheikh

unread,
Feb 9, 2012, 7:47:43 AM2/9/12
to ctjug...@googlegroups.com
static; yes,
Final: no  (compiler will complain)

Etienne Pretorius

unread,
Feb 9, 2012, 7:48:56 AM2/9/12
to ctjug...@googlegroups.com
On 09 Feb 2012, at 2:44 PM, Hendré Louw wrote:

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;


Since these a non-objects, they will act the same as the reference in the previous examples.

Static = Class variable
Final = non-mutable;

Kind Regards,
Etienne Pretorius
Reply all
Reply to author
Forward
0 new messages