What is the reason behind non-static method cannot be referenced from a static context?
You can't call something that doesn't exist. Since you haven't created an object, the non-static method doesn't exist yet. A static method (by definition) always exists.
non-static method cannot be referenced from a **static context**. What is static content here?
Your main-method is the static context, and you are trying to call the non-static method display() from it. That doesn't work even if it is in the same class. To have the disply method non-static you have to do this.
public static void main(String[] args) throws FileNotFoundException{
ReverseLL r = new ReverseLL();
r.display(head);
}
public void display(Node head){
...
}
Non-Static method cannot be referenced from a static context with methods and variables
You need to make both your method - printMenu()
and getUserChoice()
static
, as you are directly invoking them from your static main
method, without creating an instance of the class, those methods are defined in. And you cannot invoke a non-static
method without any reference to an instance of the class they are defined in.
Alternatively you can change the method invocation part to:
BookStoreApp2 bookStoreApp = new BookStoreApp2();
bookStoreApp.printMenu();
bookStoreApp.getUserChoice();
non static method cannot be referenced from a static context JPA Java
The problem is that you're trying to call the instance method createStudent()
from a static context in main()
. If you change your createStudent()
method to be static, you should be good to go:
public static Student createStudent(String student_id, Date dob, String gender, String nationality, Type_Name name) {
// ... And so on
}
EDIT: OP pointed out that this change alone gives him another error when accessing the variables em
and emf
. To fix that, you'd need to make those variables static
, too:
static EntityManager em;
static EntityManagerFactory emf;
At that point, everything in your class is static. Assuming this is a simple one-off or example -- which I'm comfortable assuming since the class is called Main
-- making everything static is just fine. In all, the code would look like this:
package grade_db;
import bean.Student;
import bean.Type_Name;
import bean.University;
import java.util.Date;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
/**
*
* @author Sam
*/
public class Main {
static EntityManager em;
static EntityManagerFactory emf;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
EntityManagerFactory emf = Persistence.createEntityManagerFactory("db/grades.odb");
EntityManager em;
em = emf.createEntityManager();
createStudent("stu00001", new Date(631152000000)), "m", "WB", new Type_Name("Bob", "", "Smith"));
em.close();
emf.close();
}
public static Student createStudent(String student_id, Date dob, String gender, String nationality, Type_Name name){
Student stu = new Student();
stu.setDob(dob);
stu.setGender(gender);
stu.setName(name);
stu.setNationality(nationality);
stu.setCampus_id("cam00001");
stu.setCourse_id(null);
stu.setStudent_id(student_id);
em.persist(stu);
return stu;
}
}
Related Topics
The Asynctask API Is Deprecated in Android 11. What Are the Alternatives
Com.Android.Build.Transform.API.Transformexception
What Is the Most Appropriate Way to Store User Settings in Android Application
Custom Fonts and Xml Layouts (Android)
How to Write to a Folder on an Sd Card in Android
How to Do a Synchronous Request With Volley
Override Back Button to Act Like Home Button
How to Declare a Variable in Gradle Usable in Java
Firebase Android Listview Not Being Displayed
Android Activity Classnotfoundexception - Tried Everything
How to Parse Xml Using the Sax Parser
Passing Data Through Intent Using Serializable
How to Parse Jsonarray in Android
How to Change the Decimal Separator of Decimalformat from Comma to Dot/Point
How to Count the Number of Documents Under a Collection in Firestore