This week's giveaway is in the JDBC forum.
We're giving away four copies of Java Database Connections & Transactions (e-book only) and have Marco Behler on-line!
See this thread for details.
Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

C++ friend functionality in JAVA  RSS feed

 
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does anyone know how to implement an equivalent functionality as the C++ friend keyword?
For example,
I have a source file in the directory /java/pa/AClass.java

And another source file in the directory /java/pb/BClass.java

Any idea on how I give access to aString for BClass only? I'll even settle with giving all classes in the same package (i.e. package java.pb) as class BClass access to aString.
Thanks,
-Peter

[This message has been edited by Peter Tran (edited January 12, 2001).]
[This message has been edited by Peter Tran (edited January 12, 2001).]
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only way to access a private function or instance variable in a class is as an inner class. Create your BClass and include your AClass as an inner class. That way BClass can directly access the String without you having to expose it to any other class.
Hope this helps
[This message has been edited by Carl Trusiak (edited January 13, 2001).]
 
Peter Tran
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Carl,
It looks like you and I had the same idea. The approach I took was to create an interface class in the same package as BClass. I then created an anonymous inner class implementing the interface. The idea was since the interface was in the package B, then only B class an any class in pb would have access to this method only. The problem I ran into was any method in an interface is public by default. Therefore, I can't restrict access to the String.
E.g.

Modified AClass to implement BInterface:

-Peter
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Peter,
I was thinking more along the lines of

This way, only BClass and AClass can access the private member aString.
Hopefully this is something like what you are looking for
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carl,
I think what Peter is looking for is some way to restrict access to AClass so that only a class in a specific other package can access it. So, while your idea works for AClass and BClass in the same package, it won't do what Peter wants.
If there's a way to do this, it's going to be pretty ugly.
Susan

[This message has been edited by Susan Hoover (edited January 15, 2001).]
 
Live a little! The night is young! And we have umbrellas in our drinks! This umbrella has a tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!