About the private member

P

pek

Here is a snippet:

class MyClass {
private int privateInt;

public void violateEncapsulation( MyClass e ) {
e.privateInt = 5;
}
}

Yes, this code works perfectly. The thing is, why? Somebody pointed me
to this: "The private modifier specifies that the member can only be
accessed in its own class." from http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html

OK.. Now I kinda get it. But is there anybody out there that actually
used this type of code? Is it just Java or OO in general? Either case,
where does this help?

Thank you.
 
L

Lew

pek said:
Here is a snippet:

class MyClass {
  private int privateInt;

  public void violateEncapsulation( MyClass e ) {
    e.privateInt = 5;
  }

}

Yes, this code works perfectly. The thing is, why?

Because the JLS says so.
Somebody pointed me to this: "The private modifier specifies that the member can only be
accessed in its own class."
from http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html

Yeah, that's what the JLS says. So the class accesses a private
member of an instance of itself. That's what is supposed to happen.
OK.. Now I kinda get it. But is there anybody out there that actually
used this type of code? Is it just Java or OO in general? Either case,
where does this help?

"OO in general" supports the notion of encapsulation by definition,
but the semantics of the 'private' access modifier in Java are
specific to Java.

One place where this is useful is in a copy constructor or method.

It is the nature of robust languages to allow all sorts of powerful
things, including things that one should never or infrequently do.
 
T

Tom Anderson

Here is a snippet:

class MyClass {
private int privateInt;

public void violateEncapsulation( MyClass e ) {
e.privateInt = 5;
}
}

Yes, this code works perfectly. The thing is, why? Somebody pointed me
to this: "The private modifier specifies that the member can only be
accessed in its own class." from http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html

OK.. Now I kinda get it. But is there anybody out there that actually
used this type of code? Is it just Java or OO in general? Either case,
where does this help?

I have to say, at some point in my learning of java this came as a huge
surprise to me too. I'd always (and incorrectly) thought of the access
controls as applying to *objects* - but they don't, they apply to
*classes*. That had never occurred to me before.

It's useful whenever writing methods that are about the interaction of an
object with its classmates. Common examples are copy constructors, equals,
and compareTo. For example:

public class Point {
private int x ;
private int y ;
public Point(int x, int y) {
this.x = x ;
this.y = y ;
}
public Point(Point p) {
this(p.x, p.y) ; // copy constructor
}
public boolean equals(Object obj) {
if ((obj != null) && (obj instanceof Point)) {
Point that = (Point)obj ;
return (this.x == that.x) && (this.y == that.y) ; // equals
}
else {
return false ;
}
}
}


tom
 
M

Martin Gregorie

OK.. Now I kinda get it. But is there anybody out there that actually
used this type of code? Is it just Java or OO in general? Either case,
where does this help?
I do that all the time in both Java and ANSI C:

----example.c------
include "example.h" /* contains function prototypes */

int _global; /* Underscore restricts scope to this file */

void example(int g)
{
_global = g;
}

int get_global()
{
return _global;
}

-------------------
which is more or less exactly equivalent to a Java singleton with a
parameterised constructor and a getter. If more than one "instance" is
needed per program, its easy to define the globals in a struct and to
name it with a typedef. The 'constructor' function mallocs space for it
and returns a pointer. It is freed by a "destructor". The pointer is
passed to all the other functions in just the same way that FILE*
pointers get passed to file handling library functions.

I think you'll find a lot of people use a similar approach to writing C.

Its close enough to OO for translating between this form of C source
module and a Java class to be a fairly straight forward process.
 
A

Arne Vajhøj

Martin said:
I do that all the time in both Java and ANSI C:

----example.c------
include "example.h" /* contains function prototypes */

int _global; /* Underscore restricts scope to this file */

void example(int g)
{
_global = g;
}

int get_global()
{
return _global;
}

-------------------
which is more or less exactly equivalent to a Java singleton with a
parameterised constructor and a getter. If more than one "instance" is
needed per program, its easy to define the globals in a struct and to
name it with a typedef. The 'constructor' function mallocs space for it
and returns a pointer. It is freed by a "destructor". The pointer is
passed to all the other functions in just the same way that FILE*
pointers get passed to file handling library functions.

I think you'll find a lot of people use a similar approach to writing C.

Its close enough to OO for translating between this form of C source
module and a Java class to be a fairly straight forward process.

Underscore does what ?

I thought you used static for that ?

Arne
 
P

pek

Underscore does what ?

I thought you used static for that ?

Arne

Sooooo.... This is not that useful (or cool) as it sounded right?
Indeed, I have only seen this in the equals method.
 
M

Martin Gregorie

Underscore does what ?
Sorry - that's a hang-over from K&R C, where globally declared variables
were truly global unless their name started with an underscore.

In ANSI any variable declared in a .c source file outside a function body
and without an 'extern' qualifier is global to the functions declared in
the same file and inaccessible outside it.
I thought you used static for that ?
'fraid not. static variables are declared inside functions. The 'static'
keyword moves them off-stack to preserve the value between function calls
but the scope remains limited to the function body.
 
A

Arne Vajhøj

pek said:
Sooooo.... This is not that useful (or cool) as it sounded right?

That I don't know, but the comment about the functionality of
underscore is wrong.

Arne
 
M

Martin Gregorie

Sooooo.... This is not that useful (or cool) as it sounded right?
Indeed, I have only seen this in the equals method.
Au contraire, its extremely useful.

In general you don't want class attributes to be visible from the
outside, so you declare them private. Why would you want that? Well, for
one thing this gives you the freedom to completely rewrite the innards of
a class without affecting its callers. If its guts are visible to the
world you can't do that without running the risk of breaking somebody
else's code.

Another case: you may may want to name a few values used by the class to
its use clearer: these do need to be accessible from outside so they are
declared as public:

public class LightSwitch
{
public final boolean ON = true;
public final boolean OFF = true;

private boolean switchState = OFF; // Default the switch to off

....
}

Finally, if you're writing a generic class that you expect to be
extended, it may have class attributes that subclasses can access but are
not visible to anybody else. These are declared as protected.
 
A

Arne Vajhøj

Martin said:
Sorry - that's a hang-over from K&R C, where globally declared variables
were truly global unless their name started with an underscore.

Ah.

But hopefully there are no K&R as in pre-ANSI compilers left.
In ANSI any variable declared in a .c source file outside a function body
and without an 'extern' qualifier is global to the functions declared in
the same file and inaccessible outside it.

'fraid not. static variables are declared inside functions. The 'static'
keyword moves them off-stack to preserve the value between function calls
but the scope remains limited to the function body.

That is the effect of using static on a local variable.

If you put static on a global variable, then it is only
visible within the file.

Arne
 
M

Mark Space

Arne said:
That is the effect of using static on a local variable.

If you put static on a global variable, then it is only
visible within the file.

Martin may be correct. I dimly recollect that the way the 'static'
modifier was commonly implemented in many early compilers was just to
flag the variable by prepending an underscore. The linker would then
treat such variables as special and not add them to the global symbol table.

It's an evil hack to use an underscore directly like that, of course.
 
M

Martin Gregorie

Ah.

But hopefully there are no K&R as in pre-ANSI compilers left.
I still have one :)

It came with my copy of Microware's OS/9 v2.4 operating system. I got the
system in 1992 and its still running perfectly on a 25MHz 68020.

Now, back to C. The ANSI spec says that variables can't start with an
underscore. However, the current version of the GNU C compiler will
accept both K&R and ANSI source and I imagine there are others that would
also do this. Its a compatibility issue: there's still quite a bit of K&R
code washing round.

BTW, I misunderstood the K&R convention about underscores. As there was
only one global namespace, there was also a convention that library code
should use leading underscores for globals to avoid name clashes with non-
library code.

The method of controlling global scope is common to K&R and ANSI C: an
unqualified global's scope is the source file. This is extended to the
whole program by the 'extern' qualifier. However, as the rules say that
all 'extern' declarations must map onto a single unqualified declaration
*somewhere* in the linked program, the situation isn't as clear-cut as
either edition of K&R makes out.
If you put static on a global variable, then it is only visible within
the file.
Not according to the first ANSI definition I looked at. I've just looked
at the 2nd edition of K&R for confirmation (the ANSI C book) and it
agrees with you. In summary, this is the situation. There are three ways
to declare a global:

int global; /* 1 scope is the source file */
static int global; /* 2 scope is the source file. This variable cannot
be used as the base for 'extern' references. */
extern int global; /* 3 scope is the whole program but this is
only a reference. The actual variable must
be declared elsewhere using format (1) */

For a variable declared inside a function 'static' works as previously
discussed.
 
M

Martin Gregorie

Martin may be correct. I dimly recollect that the way the 'static'
modifier was commonly implemented in many early compilers was just to
flag the variable by prepending an underscore. The linker would then
treat such variables as special and not add them to the global symbol
table.
I just dug out my copy of the original K*&R C book. It doesn't say
anything about special meanings for leading underscores, but I'm certain
I read it somewhere: its quirky enough not to be the sort of thing I'd
imagine.
It's an evil hack to use an underscore directly like that, of course.
Indeed, but in a traditional four-pass C compiler what else can you do
since there are no symbol tables etc. that are common to the pre-
processor and the C to assembler translation phase.
 
A

Arne Vajhøj

Martin said:
Not according to the first ANSI definition I looked at.

It does not have a section 3.1.2.2 with:

<quote>
In the set of translation units and libraries that constitutes an
entire program, each instance of a particular identifier with external
linkage denotes the same object or function. Within one translation
unit, each instance of an identifier with internal linkage denotes the
same object or function. Identifiers with no linkage denote unique
entities.

If the declaration of an identifier for an object or a function has
file scope and contains the storage-class specifier static, the
identifier has internal linkage.
> I've just looked
at the 2nd edition of K&R for confirmation (the ANSI C book) and it
agrees with you.

Good.

Arne
 
R

Roedy Green

OK.. Now I kinda get it. But is there anybody out there that actually
used this type of code? Is it just Java or OO in general? Either case,
where does this help?

see http://mindprod.com/jgloss/scope.html
and follow links to find out all the things you can do.

You might use code like your example in a copy constructor
or a step-up step-down constructor e.g. to convert Dog to Dalmatian or
Dalmatian to Dog.

--
Roedy Green Canadian Mind Products
http://mindprod.com
"Humanity is conducting an unintended, uncontrolled, globally pervasive experiment
whose ultimate consequences could be second only to global nuclear war."
~ Environment Canada (The Canadian equivalent of the EPA on global warming)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top