![](/img/trans.png)
[英]When an instance of a class is not created in the main method, will the default constructor be called?
[英]Instance “Main” Method Not Running When Called from Another Class
我正在學習Java並在netbeans中嘗試Javafx。
我在這里運行sqlite教程: http : //www.tutorialspoint.com/sqlite/sqlite_java.htm
當設置為一個單獨的文件時,它當然可以正常工作。
我正在測試項目“ testDB”中進行設置,由於某種原因,當我啟動該類時,可以識別該類本身,但main()未運行。
這是testdb文件本身:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package sqlitetest;
import java.sql.*;
/**
*
*/
public class testDB {
public static void main(String args[]) {
//THESE STEPS ARE ON NOT RUNNING (compiles without errors)
System.out.println("testing");
Connection c = null;
try {
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:test.db");
} catch (Exception e) {
System.err.println(e.getClass().getName() + ": " + e.getMessage());
System.exit(0);
}
System.out.println("Opened database successfully");
}
public void makeStuff(){
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package sqlitetest;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
*
*/
public class Sqlitetest extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
testDB test = new testDB();
test.makeStuff();
launch(args);
}
}
我認為您對構造函數和主要方法感到困惑。
在testdb
,更改:
public static void main(String args[]) {
至
public testdb() {
可替代地,調用testdb.main(args)
(或與一些其它參數)在Sqlitetest.main
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.