Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

friend keyword or equivalent? 1

Status
Not open for further replies.

cpjust

Programmer
Sep 23, 2003
2,132
0
0
US
Hi,
As far as I can see, Java doesn't have a friend keyword like C++ does.

I have one class that needs access to a private member variable of another class. How can I do this in Java?
 
BTW, I already know about Package-level access, but that's too much access, plus the two classes I'm using are in different packages.

If Java had a const keyword, I could make a public Get() function and simply return a const version of the member variable to ensure it can't be changed, but that's not available either... :-(
 
Private in Java is really private. Two instances of the same class can access each other's private members, but nothing else outside that class can. The next level up is protected, where only derivations of a class can access the member, then it's the package-level access you already know about.

Tim
 
You can make a public method to return the value of the private member. If it's an object and you don't want it to be changed, just return a copy.

But if the other class needs access to that object, maybe you should consider making it public.

Cheers,
Dian
 
Thanks.
I don't like making copies of object that won't be modified since it's just wasteful, but I guess that's the only thing I can do in Java.
 
Another possible way, depending on your class design, is:

A: Class of the object you want to access
B: Class that holds the type A object
C: Class that want to access type A object.

Then

A and B are in the same package (not C)
A has public get methods and package set methods.
B has a public method that returns the A object

So

B can access all A methods
C can have the A object
C can use the get methods from A but not the set ones.

Tricky, but could work.

Cheers,
Dian

 
Actually A, B & C are all in different packages, so I'm just going with the copy option. It's just a QA program, so performance isn't a big worry anyways.
 
I'm not certain but, from studying C++ without yet using it, Java's final is equivalent to const; a final variable cannot be changed.
 
From what I understood, final variables cannot be reassigned, but you can call member functions that can modify the state of that variable:
Code:
void function( final StringBuffer strBuf )
{
   strBuf = new StringBuffer();   // Not allowed.
   strBuf.append( "blah blah" );  // Allowed.
}
That is not the same as const in C++.
 
Oh, I had assumed your variable was a primitive, not an object. If you make a deep copy of it, you can pass that in a method with no risk of the original being modified; is this appropriate?

(Diancecht: That was a tentative statement.)
 
Yeah, that's what I ended up doing. Although having to do that every time I want to ensure that a variable isn't modified is a real pain in the ass... :-(

Java has 'const' as a reserved word, but they still haven't used it. I can't understand why they're so hesitent about adding useful features to the language? It took them forever just to add enums and templates (i.e. generics).
 
Hey, I just thought of another way around the lack of const in Java... I could create an interface that only exposes the methods of the class that don't change its state and return that const-like interface; or create a const-like wrapper class that does the same thing, but holds a reference to a class that I don't have control over and just expose the methods I want...
It's still a pain in the ass to have to write extra code rather than letting the compiler enforce the constness, but that's probably the best I can do in Java for now.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top