0

i coded like this

 import static adminDetails.Provider.*; import java.sql.*; public class ConnectionProvider { private static Connection con; static{ try{ Class.forName(DRIVER); con=DriverManager.getConnection(CONNECTION_URL,USERNAME,PASSWORD); }catch(Exception e){} } public static Connection getCon(){ return con; } } 

and use this code in another java file with a static method call like this

public class AdministrationDetails { Connection con=ConnectionProvider.getCon(); //... } 

In java API also they are doing static imports like this. My question is why do we need static method call instead normal object, what is the difference.

2
  • Might be a duplication of this: stackoverflow.com/questions/15183295/… Commented Aug 11, 2014 at 7:36
  • Why would you want to create objects of ConnectionProvider to get the connection object. Static methods avoid this and hence is more efficient Commented Aug 11, 2014 at 7:38

1 Answer 1

2

Connection is static to prevent recurring creation of connection objects when there is no use of multiple connections due to their stateless-ness.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.