473,407 Members | 2,306 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Private member variables

Let's take the following class:

class MyClass {
private int privateVar;
public int PublicVar {
get { return privateVar; }
}

public MyClass() {}

public MyClass Test() {
MyClass myClass = new MyClass();
myClass.privateVar = 99;
return myClass;
}
}

How is it that in the Test() method, my newly instantiated
'myClass' variable can access the private 'privateVar' member?
Because the code is executing within a method of the 'MyClass'
class? Shouldn't it be that an object cannot access private
members regardless of where it's executed? Now, that is
true if I try to execute that exact same code in a method of
another class which is as it should be. I'm just curious what
is it that's telling both the compiler and the CLR that accessing
the private member in the above method is alright.

thnx,
Christoph
Nov 16 '05 #1
2 1809
Christoph,

Private members of a class are meant to be accessed by other members of the
class. if privateVar is inaccessible by methods with its class, virtually
nothing can ever touch it. Now this doesn't make sense since anything in a
program must be used somewhere.

When we decide the accessibility of a member within a class we are looking
at a class-scope. That is, if a member is declared private, it is private to
THE CLASS so that it makes pretty good logic that the other members within
the same class should have a right to access the (private) member. Your
constructor MyClass.MyClass() actually implicitly initiates the privateVar
member without you writing a line of code.

A well designed class must maintain a proper degree of encapsulation. If
something is not meant to be used by its client then by all means make it
private. If somehow something is not meant to be used by the client but
making it private will cause the class exceedingly difficult to reuse, make
it protected.

ben
Let's take the following class:

class MyClass {
private int privateVar;
public int PublicVar {
get { return privateVar; }
}

public MyClass() {}

public MyClass Test() {
MyClass myClass = new MyClass();
myClass.privateVar = 99;
return myClass;
}
}

How is it that in the Test() method, my newly instantiated
'myClass' variable can access the private 'privateVar' member?
Because the code is executing within a method of the 'MyClass'
class? Shouldn't it be that an object cannot access private
members regardless of where it's executed? Now, that is
true if I try to execute that exact same code in a method of
another class which is as it should be. I'm just curious what
is it that's telling both the compiler and the CLR that accessing
the private member in the above method is alright.

thnx,
Christoph

Nov 16 '05 #2
I think Christoph means: Why is it that the private member is accessible
also on a local instance
of the class created within a member method, and not only for the current
object?

Example (current object):
privateVar = 99
// or
this.privateVar = 99

Example (Christoph's local variable in the Test() method)
myClass.privateVar = 99
Answer: What the private modifier actually does, is that it restrict access
to only the containing type.
This means that you can access the private member, as long as you access it
from anywhere within
the current type (MyClass methods etc.). Regardless which instance of the
type your'e accessing.
This is the correct design of an object-oriented language. (Same in the Java
language etc.).
/Emil Kvarnhammar

"benben" <be******@yahoo.com.au> wrote in message
news:OT**************@TK2MSFTNGP09.phx.gbl...
Christoph,

Private members of a class are meant to be accessed by other members of the class. if privateVar is inaccessible by methods with its class, virtually
nothing can ever touch it. Now this doesn't make sense since anything in a
program must be used somewhere.

When we decide the accessibility of a member within a class we are looking
at a class-scope. That is, if a member is declared private, it is private to THE CLASS so that it makes pretty good logic that the other members within
the same class should have a right to access the (private) member. Your
constructor MyClass.MyClass() actually implicitly initiates the privateVar
member without you writing a line of code.

A well designed class must maintain a proper degree of encapsulation. If
something is not meant to be used by its client then by all means make it
private. If somehow something is not meant to be used by the client but
making it private will cause the class exceedingly difficult to reuse, make it protected.

ben
Let's take the following class:

class MyClass {
private int privateVar;
public int PublicVar {
get { return privateVar; }
}

public MyClass() {}

public MyClass Test() {
MyClass myClass = new MyClass();
myClass.privateVar = 99;
return myClass;
}
}

How is it that in the Test() method, my newly instantiated
'myClass' variable can access the private 'privateVar' member?
Because the code is executing within a method of the 'MyClass'
class? Shouldn't it be that an object cannot access private
members regardless of where it's executed? Now, that is
true if I try to execute that exact same code in a method of
another class which is as it should be. I'm just curious what
is it that's telling both the compiler and the CLR that accessing
the private member in the above method is alright.

thnx,
Christoph


Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: IHateSuperman | last post by:
public class StaticField2{ public static void main(String args){ private int x, y; // <<== error 1 for ( y = 0 ; y < 100 ; y++){ x = StaticMethod(); System.out.println(" x = "+x); } } public...
10
by: Scott Brady Drummonds | last post by:
Hi, everyone, I'm still learning Python as I develop a medium-sized project. From my previous experience with C++, I've burnt into my mind the notion of information hiding. I'm having trouble...
3
by: Rajesh Garg | last post by:
Can we have private constructors and destructors? IF yes what is the use of such constructors or destructors.....in the sense where can these be implemented in a system................. I have...
19
by: qazmlp | last post by:
class base { // other members public: virtual ~base() { } virtual void virtualMethod1()=0 ; virtual void virtualMethod2()=0 ; virtual void virtualMethod3()=0 ;
6
by: David Whitchurch-Bennett | last post by:
Hi There, I have created a very simple web user control, containing only a combo box. I have created a class which contains some private object variables, for example... Private _anObject as...
23
by: Ben Voigt | last post by:
I have a POD type with a private destructor. There are a whole hierarchy of derived POD types, all meant to be freed using a public member function Destroy in the base class. I get warning C4624....
6
by: zfareed | last post by:
Is it possible to print an array that is declared from a class type that also contains private data members? I have declared an array in the main function and then set the values for the array...
8
by: David Veeneman | last post by:
Should a member variable be passed to a private method in the same class as a method argument, or should the method simply call the member variable? For years, I have passed member variables to...
5
by: fourpastmidnight | last post by:
Ok, before anyone gets on me ( ;) ), I'm developing on Windows 2000 with IE6sp1 (that's what my company uses). So no, I can't use Firefox, though I wish I could. Ok, with that out of the way, here's...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.