This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Possible to have Exception to Private Class? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Possible to have Exception to Private Class?" Watch "Possible to have Exception to Private Class?" New topic
Author

Possible to have Exception to Private Class?

Brad Walton
Ranch Hand

Joined: Feb 16, 2008
Posts: 35
I think the answer is no , but is there a way to make a class private, but in that private class declare an exception which will allow another class to access it's methods? I want to do this for flow control witin the program, so that all the classes in a specific package must go through a 'master' class.

Thanks for any help.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

I can't quite tell what that is all supposed to mean, but it all sounds like a bad idea to me. I guess what would help most is if you defined "go through" more clearly. Right now I have no idea what "going through" a class is supposed to do or what benefits you think it would have. Flow control? I don't get that either.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

I don't think I get it either.

You can have a "master" class with private variables referencing instances of these other classes. Would that accomplish what you're after?


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Brad Walton
Ranch Hand

Joined: Feb 16, 2008
Posts: 35
Thanks for the replies.. let me try to clarify what I mean. I am trying to 'lock down' the code a bit, to help make bugs easier to track down, and to keep a handle on the flow (how data is traversing the program). This is a mid-sized app worked on by myself and 2 other people (one of which speaks very little English). It has been setup where all the controller classes are package visible, but there is a main controller class which is public visible and contains a gateway to all the methods in the package controllers that should be accessible outside of that scope. There are also many package static's in the main controller. The idea behind this design was to prevent other packages from acting as controllers, and catch any that do. It has worked to some extent, but I'd like to take that a step further and lock down each individual class in the controller package as private, but still allow the main controller piece to call the private class' methods. It's an insurance policy I guess...

In a nutshell, I�m trying to make sure our small group of developers follow the design, catch their mistakes, and aid in tracking bugs. Does that make more sense?

Thanks again!

Edit: Included an image which might help with clearing up what I'm asking. ControllerA, B, C, and D can only be accessed by the MainController. And, all of these are in the same package. These are static classes (not instantiated objects). These classes are acting upon objects which are stored in the MainController.


[ May 02, 2008: Message edited by: Brad Walton ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38064
    
  22
Agree with Marc and Paul. What you have written appears to be a contradiction in terms in Java, though maybe not in other languages which have more granularity of access control.

No object can "claim access" of an object of another class; the other class grants access. If you have 4 controllers in the same package with package-private access, then all 4 other classes on your diagram will have access to them. If you really want to restrict access by making the other classes private, they would have to be inner classes of the MainController class, and might still have access to one another.
Brad Walton
Ranch Hand

Joined: Feb 16, 2008
Posts: 35
Yeah I thought that was the case but just wanted to make sure. Thanks again for the info.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Well... I suppose you could make these private inner classes. But that doesn't seem like a good plan.
Brad Walton
Ranch Hand

Joined: Feb 16, 2008
Posts: 35
No that would not be a good solution in this case.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Possible to have Exception to Private Class?
 
Similar Threads
how to initialize static final field that the constructor throws some exception
WSAD5.1.1 :WSWS3324E error in generating wsdl files
bout top-level class
Exception Handling In Instance Initializer Blocks
Can a static final constant throw an exception?