저는 자바를 배우려고 애 쓰고 있습니다. 한 클래스에서 다른 클래스로 함수를 호출하는 방법을 이해하려고 시도하고 있습니다. 나는 아래에 게시 할 두 개의 수업을 가지고 있습니다. ToDo 클래스에서 H2db.addItems()를 호출 할 때 및 H2db 클래스에서 ToDo.menu()를 호출 할 때 "정적이 아닌 메서드를 정적 컨텍스트에서 참조 할 수 없습니다."가 나타납니다. 인터넷 검색은 인스턴스 (?)와 관련이 있다고 밝혔지만 그 의미가 확실하지 않습니다.정적 함수와 인스턴스가이 경우 어떻게 함께 작동합니까?
클래스 할일 :
package com.company;
import java.io.*;
import java.lang.reflect.Method;
import java.sql.*;
import java.util.ArrayList;
import java.util.Scanner;
public class ToDo {
//BufferedReader inputread = new BufferedReader(new InputStreamReader(System.in));
Scanner inputread = new Scanner(System.in);
ArrayList<String> toDoList = new ArrayList<String>();
public void menu() {
clearConsole();
System.out.println("Welcome to the To-Do program.");
System.out.println();
System.out.println();
System.out.println("Please select an option from the following menu, using the number.:");
System.out.println("1- View To-Do List");
System.out.println("2- Add Item To List");
System.out.println("3- Remove Item From List");
System.out.println("4- Save List");
System.out.println("5- Load List");
int userinput = inputread.nextInt();
switch (userinput) {
case 1:
clearConsole();
displayList(true);
menu();
break;
case 2:
clearConsole();
H2db.addItems();
break;
case 3:
clearConsole();
deleteItem();
break;
case 4:
clearConsole();
try {
saveList();
}
catch(IOException e){
return;
};
case 5:
clearConsole();
try {
loadList();
}
catch(IOException e){
return;
};
}
}
public void clearConsole() {
for (int i = 0; i < 25; i++) {
System.out.println();
}
}
public void addItem() {
System.out.println("Please type the item to add to the To-Do List");
//The skip gets over the leftover newline character
inputread.skip("\n");
String newItem = inputread.nextLine();
toDoList.add(newItem);
System.out.println("Your item has been added! Type any key and press Enter to continue");
String discardMe = inputread.next();
menu();
}
public void displayList(boolean finish) {
if (toDoList.isEmpty()) {
System.out.println("Add an activity.");
} else {
for (String listItem: toDoList) {
System.out.println(listItem);
}
}
if (finish) {
System.out.println();
System.out.println("This is your list. Type any key and press Enter to continue");
String discardMe = inputread.next();
}
}
public void deleteItem() {
if (toDoList.isEmpty()) {
System.out.println("For fuck's sake, add an activity.");
} else {
System.out.println("Please choose the number of the line you want to delete:");
displayList(false);
int userinput = inputread.nextInt();
int listPos = userinput - 1;
toDoList.remove(listPos);
System.out.println("That item has been deleted. Type any key and press Enter to continue.");
String discardMe = inputread.next();
}
menu();
}
public void saveList() throws IOException {
System.out.println("Saving list...");
PrintWriter writer = new PrintWriter("list.txt", "UTF-8");
for (String listItem : toDoList) {
writer.println(listItem);
}
writer.close();
System.out.println("List saved! Type any key and press Enter to continue");
String discardMe = inputread.next();
menu();
}
public void loadList() throws IOException {
System.out.println("Loading list...");
File loadFile = new File("list.txt");
Scanner scanner = new Scanner(loadFile);
while (scanner.hasNext()) {
String item = scanner.nextLine();
toDoList.add(item);
}
System.out.println("List loaded! Type any key and press Enter to continue");
String discardMe = inputread.next();
menu();
scanner.close();
}
}
클래스 H2db :
package com.company;
import java.sql.*;
import java.util.ArrayList;
import java.util.Scanner;
public class H2db {
Scanner inputread = new Scanner(System.in);
ArrayList<String> toDoList = new ArrayList<String>();
public void connect(){
try {
Class.forName("org.h2.Driver");
Connection conn = DriverManager.getConnection("jdbc:h2:~/toDo", "user", "");
Statement stat = conn.createStatement();
stat.execute("CREATE TABLE IF NOT EXIST 'todo'(id int NOT NULL AUTO_INCREMENT primary key, item varchar(255))");
}
catch(Exception e) {
System.out.println(e.getMessage());
}
}
public void addItems() {
PreparedStatement addstat;
try {
Class.forName("org.h2.Driver");
Connection conn = DriverManager.getConnection("jdbc:h2:~/toDo", "user", "");
System.out.println("Please type the item to add to the To-Do List");
inputread.skip("\n");
String newItem = inputread.nextLine();
addstat = conn.prepareStatement("INSERT INTO toDo (item)" +"VALUES (?)");
addstat.setString(1, newItem);
addstat.executeUpdate();
System.out.println("Your item has been added! Type any key and press Enter to continue");
String discardMe = inputread.next();
ToDo.menu();
}
catch(Exception e) {
System.out.println(e.getMessage());
}
}
}
어떤 도움이 좋지 않을까, 감사합니다. 나는 이것이 지식의 격차라는 것을 알고 있지만, 나는이 개념이 무엇이든간에 무엇이든 파악할 수는 없다.
정적으로 선언하는 곳은 어디입니까? –
[정적이 아닌 변수는 정적 컨텍스트에서 참조 할 수 없습니다] (http://stackoverflow.com/questions/2559527/non-static-variable-cannot-be-referenced-from-a-static-context) – Johan
@Sridhar DD 아니, 그게 문제 야. 메서드를 정적으로 변경하려고하면 다른 모든 메서드가 중단됩니다. ToDo의 인스턴스와 함께 사용하여 정적 일 필요가없는 방법이 있습니까? 내가 말했듯이, 그 개념은 저를 넘어서는 것입니다. –