aspose file tools*
The moose likes Beginning Java and the fly likes Help on objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help on objects" Watch "Help on objects" New topic
Author

Help on objects

Jennifer Sohl
Ranch Hand

Joined: Feb 28, 2001
Posts: 455
Hi. I have three different classes I am working with. 1. OrdEntMenu 2.LogonDialog 3.SQLConnection
OrdEntMenu is a main order entry menu. LogonDialog is a Dialog box that is supposed to come up so the user can sign on to the database, and SQLConnection is the database connection class.
I am having a hard time figuring out what I need to do to make these three classes work together.
Here is the main method for OrdEntMenu:

Here is code for my SQLConnection class:


My problem lies in the line where I am calling methods on the LogonDialog class.
I know it is not initialized here. However, if I initialize it aren't I creating a totally different object than the one I created in my OrdEntMenu class?
I need to get to the object of the LogonDialog class that I created in the OrdEntMenu class so I can get the text the user keyed so I can pass that to the database connection object.
I hope this is understandable.
Am I going about this all wrong, or is it something simple I am missing?
Thanks for any help in advance!
[ March 04, 2002: Message edited by: Jennifer Sohl ]
Alex Ku
Ranch Hand

Joined: Jan 15, 2002
Posts: 47
Hi,
1. you can make LogonDialog ld as a global varible,
static LogonDialog ld = new LogonDialog();
then you can access it by MAINCLASS.ld.METHODNAME (if ld is a static varible in MAINCLASS class)
2. Or you can pass ld to OrdEntMenu, either to its construct or using a setter,
LogonDialog ld = new LogonDialog();
OrdEntMenu tr = new OrdEntMenu(ld);
or
tr.setLogonDialog(ld);
kawaii
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4456
    
    6

A very important Design Principle: keep your classes loosely coupled. IOW, each class should know as little as possible about other classes. Google for "Demeter's Law" for guidelines on how to maintain proper knowledge of other classes. DO NOT use a global static variable.
Passing LoginDialog as a parameter to a setXXX method is somewhat OK but the ideal way would be to just pass the username and password Strings as parameters to the SQLConnection constructor. SQLConnection does not really need to care about the source of the username and password and by making it know about LoginDialog, you couple the two classes unnecessarily and inappropriately.
Jennifer Sohl
Ranch Hand

Joined: Feb 28, 2001
Posts: 455
Thanks for the replies. In reference to passing the username and password "Strings" to the SQL Connection constructor...
I'm a bit confused. Don't I still have to pass LoginDialog to the constructor? If I pass username and password into the constructor as String objects, how does it know I mean the username and password Strings from the LogonDialog object?
I seem to be a bit confused.
Could someone please straighten me out?
Thanks a million! I really appreciate it.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4456
    
    6

Don't I still have to pass LoginDialog to the constructor? If I pass username and password into the constructor as String objects, how does it know I mean the username and password Strings from the LogonDialog object?
The point is, does SQLConnection really have to care where the userName and password came from? All it cares is that it has these values to make the connection, right? So, instead of extracting the strings from the dialog inside the SQLConnection constructor, extract them inside the LogonDialog event handler. The LogonDialog event handler more appropriately has intimate knowledge of LogonDialog whereas SQLConnection does not.

Junilu
[ March 06, 2002: Message edited by: Junilu Lacar ]
Jennifer Sohl
Ranch Hand

Joined: Feb 28, 2001
Posts: 455
OH!!! Now I see what you're saying. I had myself completely turned around. Looks like another case of making something harder than it really is. I'm good at that.
Thanks SO much for your help!! You just made my day a little brighter!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help on objects