这里有一个静态实现,我不明白。我以前使用过静态但不广泛,任何人都可以帮助我理解代码。这是代码
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Connection_Class {
String driver_ClassName="com.mysql.jdbc.Driver";
String URL_connection="jdbc:mysql://localhost:3306/vendor";
String user="root";
String password="lifesuckzz";
//can anybody explain what the following line means, especially the static part.......
private static Connection_Class connectionclass=null;
private Connection_Class(){
try{
Class.forName(driver_ClassName);
}catch(ClassNotFoundException e){
e.printStackTrace();
}
}
public Connection getConnection() throws SQLException{
Connection con=null;
con=DriverManager.getConnection(URL_connection,user,password);
return con;
}
public static Connection_Class getInstance(){
if(connectionclass==null){
//I know its returning an instance here
connectionclass=new Connection_Class();
}
return connectionclass;
}
}
最佳答案
static
表示该变量是类本身的成员(只有一份副本),而不是该类的对象的成员(每个对象一个)。您可以在没有对象的情况下访问静态变量。在这种情况下,您可以调用 Connection_Class.getInstance()
来获取整个程序共享的单个 Connection_Class
对象。
关于java - private static ClassName ClassInstanceVariableName=null;.....这是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16788548/