Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Accessing class member variables - properties or variables?

Reply
Thread Tools

Accessing class member variables - properties or variables?

 
 
dwok
Guest
Posts: n/a
 
      03-03-2005
I have been wondering this for a while now. Suppose I have a class
that contains some private member variables. How should I access the
variables throughout the class? Should I use properties that expose the
variables or is it OK to just access the variables directly? Keep in
mind that I am talking about accessing the variables from within the
class that they are defined. Thanks!

 
Reply With Quote
 
 
 
 
klynn47@comcast.net
Guest
Posts: n/a
 
      03-03-2005
Regardless of what level of access you give an instance variable, it
can be accessed in any instance method in the class definition.

 
Reply With Quote
 
 
 
 
Ken Tucker [MVP]
Guest
Posts: n/a
 
      03-03-2005
Hi,

I would access the variables directly from within the class. Use
properties for accessing them from outside the class.

Ken
-----------------
"dwok" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
I have been wondering this for a while now. Suppose I have a class
that contains some private member variables. How should I access the
variables throughout the class? Should I use properties that expose the
variables or is it OK to just access the variables directly? Keep in
mind that I am talking about accessing the variables from within the
class that they are defined. Thanks!


 
Reply With Quote
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      03-03-2005
"dwok" <(E-Mail Removed)> schrieb:
> I have been wondering this for a while now. Suppose I have a class
> that contains some private member variables. How should I access the
> variables throughout the class? Should I use properties that expose the
> variables or is it OK to just access the variables directly? Keep in
> mind that I am talking about accessing the variables from within the
> class that they are defined.


I think this is the way most developers do it.

Nevertheless, I use 'Static' [VB.NET] variables inside methods whenever it
makes sense to use them, and I never access member variables which are
holding property values directly, except in the property's 'Get' or 'Set'
block.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

 
Reply With Quote
 
Hal Rosser
Guest
Posts: n/a
 
      03-03-2005

"dwok" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> I have been wondering this for a while now. Suppose I have a class
> that contains some private member variables. How should I access the
> variables throughout the class? Should I use properties that expose the
> variables or is it OK to just access the variables directly? Keep in
> mind that I am talking about accessing the variables from within the
> class that they are defined. Thanks!


I assume by 'member' variables, you mean 'instance' variables (which can be
thought of as 'properties' of an instance of the class). <<<as a general
rule>>> Normally, instance variables are declared private and normally,
the 'get' and 'set' methods to access them are declared public. individual
mileage may vary.
'Static' variables (and constants) belong to the class without regard to any
instance (VB.NEt's Color.Blue for example).


 
Reply With Quote
 
Bruce Wood
Guest
Posts: n/a
 
      03-03-2005
It all depends upon the situation.

For example, I have a class in which setting a property through the
"set" method causes the class to fire an event saying that the value
changed. Sometimes, from within the class I want to set it in this way;
if I forget, any subscribers won't know that the value changed. Other
times, I definitely want to just set the value, without firing an
event.

What I'm driving at here is that properties often do more than just set
the value of a private member: they do additional processing. This is
particularly common in WinForms applications in which objects must
notify the UI that something has changed and needs to be redisplayed.

In those cases in which a property does nothing more than set the
member variable, most developers just set the member variable directly.

 
Reply With Quote
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      03-03-2005
Hal,

"Hal Rosser" <(E-Mail Removed)> schrieb:
> 'Static' variables (and constants) belong to the class without regard to
> any
> instance (VB.NEt's Color.Blue for example).


In VB.NET, variables marked as 'Static' belong to the method they are
decalared in:

\\\
Private Sub Foo()
Static x As Integer
...
End Sub
///

Properties like 'Color.Blue' are shared properties ('Shared' modifier).

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

 
Reply With Quote
 
Hal Rosser
Guest
Posts: n/a
 
      03-04-2005

"Herfried K. Wagner [MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hal,
>
> "Hal Rosser" <(E-Mail Removed)> schrieb:
> > 'Static' variables (and constants) belong to the class without regard to
> > any
> > instance (VB.NEt's Color.Blue for example).

>
> In VB.NET, variables marked as 'Static' belong to the method they are
> decalared in:
>
> \\\
> Private Sub Foo()
> Static x As Integer
> ...
> End Sub
> ///
>
> Properties like 'Color.Blue' are shared properties ('Shared' modifier).


Oh YEAH! you're right! I should not have inserted a reference from VB, since
I was answering the OP in terms of Java. - In VB, 'static' refers to a
variable that keeps its value from call to call - and is really not
applicable in the context of this thread. I was answering the OP in terms of
Java (but foolishly inserted a VB reference - ie: Color.Blue )
In Java - the Color.Blue reference analogy would be referring to a variable
named "Blue" which was declared "static" in the class named "Color". Hence
the ability to reference "Blue" without creating an instance of the "Color"
class.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Use of Namespaced Class & accessing Member variables in a CallbackFunction jawaharlal@gmail.com Javascript 0 07-23-2008 11:16 PM
Put variables into member variables or function variables? tjumail@gmail.com C++ 9 03-23-2008 04:03 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
Can Derived class static member access protected member from base class? Siemel Naran C++ 4 01-12-2005 06:46 PM
Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class. DJ Dev ASP .Net 3 02-08-2004 04:19 PM



Advertisments