Results 1 to 9 of 9
  1. #1
    saul_2110 is offline Member
    Join Date
    Dec 2008
    Location
    Mexico
    Posts
    10
    Rep Power
    0

    Question Is it acceptable to use a private object?

    Hi. I have a doubt; in the code below, object B is using (but not modifying) a private member of A. Is that acceptable for java's style of programming?

    Java Code:
    public class C1
    {
       private Connection conn;
    
       C1()
       { 
          Class.forName("com.mysql.java.Driver").newInstance();
          conn = DriverManager.getConnection("jdbc:...","user","password");
       }
    
       public Connection getConn() { return conn; }
    
       public static void main(String[] args)
       {
          C1 A = new C1();
          C2 B = new C2();
    
          B.getFromDatabase( A.getConn() );
       }
    }
    
    class C2
    {
       public void getFromDatabase(Connection conn)
       {
          Statement stmt = conn.createStatement(); // <-- Use of private member of class C1
          ResultSet rs = stmt.executeQuery( "SELECT * FROM STATES" );
          /* do something clever */
          rs.close();
          stmt.close();
       }   
    }
    Note: I removed the import statements and exception handling on purpose.
    Last edited by saul_2110; 12-06-2008 at 07:12 PM. Reason: To make the issue clearer

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    what happens when you compile it?

  3. #3
    saul_2110 is offline Member
    Join Date
    Dec 2008
    Location
    Mexico
    Posts
    10
    Rep Power
    0

    Default

    Initially I had just compiled it and it was OK. To be sure, I connected to the actual DB and retrieved data. It worked OK, too.

    The question left is whether it is a good programming style or should it be avoid?

  4. #4
    timkd127 is offline Member
    Join Date
    Dec 2008
    Posts
    41
    Rep Power
    0

    Default

    iv'e never heard of a private aggregate field being bad style, but if you dont like it you could add a copy constructor to the Connection class and pass a copy to the class your working in. Ex

    public Connection(Connection i ){

    conn.field=field;//field being a field of the Connection class, use as many as neccessary.
    }
    use that constructor to initialize the aggregate object and that will take care of the security hole so you dont have to use a private field.

  5. #5
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    Looking over your code, I think you might wanna look at it from a different angle.

    Do you know about the "static" modifier? Just wondering, because after looking at your code, it seems like it would make more sense to take advantage of class variables and methods.

    If you don't, then read up on it. Basically "static" variables and methods belong to the whole class (there is only one of each), while non-static (instance) variables and methods belong to the specific instance of the class (and there exist one for each object created).

  6. #6
    saul_2110 is offline Member
    Join Date
    Dec 2008
    Location
    Mexico
    Posts
    10
    Rep Power
    0

    Default

    The problem I "feel" that exists in the code is that "Class C1 is giving access of its private object X to other classes, by means of the getter method of X"... (where X is "conn" in the example)

    Now, the reason I feel this is a problem is that, in general, having a getter method for a private object member exposes its public interface, as if it were declared public in the class... and that is a security hole [isn't it?]

    The existance of that security hole would make it a bad programming style or at least a bad practice, right?

    In the example, by exposing A's private member "conn", object B (of class C2) is able to close A's connection with the DB.

  7. #7
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    Well you have to work with getter methods if you want to use a private attribute. As long as your not using any setter methods you should be fine. Take a bank account and account number for example.

    The account number has to be private because we obviously don't want people changing their account numbers around, but we also need to be able to see what it so that money can be deposited into the right account. If accountNumber was public we could easily change it using

    instanceOfClass.accountNumber = myAccountNumber

    whereas if it were private

    instanceOfClass.getAccountNumber() = myAccountNumber

    would throw errors since getAccountNumber would only return the string representation of that number(not the actual string attribute attached to that object)

  8. #8
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    Oh and to actually answer your question yes it should be perfectly acceptable(unless your employee is super crazy on program security and wants absolutely no reference to private fields)

    Also something to note; if DriverManager is a class you made be wary what fields you make available using getters.

    A.getConn() can easily be made something.Decrypt(A.getConn().getUser().getPasswor d())

  9. #9
    saul_2110 is offline Member
    Join Date
    Dec 2008
    Location
    Mexico
    Posts
    10
    Rep Power
    0

    Default

    Quote Originally Posted by xcallmejudasx View Post
    Well you have to work with getter methods if you want to use a private attribute.
    Well, in the case of primitive attributes it would be OK as you showed in the bank example, but what if the attribute is an object, which has its own public interface.

    In that scenario, anyone may misuse the public interface of the private object, like calling the close() method of the Connection object in the example code.

Similar Threads

  1. How to implement private chat application using jsp
    By PortalTeam in forum JavaServer Pages (JSP) and JSTL
    Replies: 9
    Last Post: 02-19-2010, 10:49 PM
  2. Private Classes Clarification
    By justlearning in forum New To Java
    Replies: 1
    Last Post: 05-06-2008, 10:51 PM
  3. Private main method
    By bugger in forum New To Java
    Replies: 1
    Last Post: 12-21-2007, 09:45 AM
  4. Question of private member
    By Felissa in forum Advanced Java
    Replies: 2
    Last Post: 06-28-2007, 09:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •