Java - 对非静态方法进行静态引用

时间:2016-01-11 20:45:00

标签: java hibernate static

对于一个项目,我正在尝试创建一个可以使用hibernate从数据库中提取数据的类。

在这个类中,我还尝试使用通过DAO从数据库中提取的数据制作一个对象列表。但是当我尝试循环遍历列表时,eclipse提醒我我正在尝试对非静态方法进行静态引用。

我的印象是,如果你使用一个不是静态的对象实例(在这种情况下是操作符op),并且它没有引用它可以工作的静态方法。 为什么这不起作用,我该如何解决?

让我特别困惑的是,老师给出的示例代码做了同样的事情,但确实有效。

package Client.DAO;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

public class mainTest {

private static EntityManagerFactory entityManagerFactory;

public static void main(String[] args) {

    EntityManager em = null;
    try {
        entityManagerFactory = HibernateUtil.getEntityManagerFactory();
        em = entityManagerFactory.createEntityManager();
    } catch (Throwable ex) {
        System.err.println("Failed to create sessionFactory object." + ex);
        throw new ExceptionInInitializerError(ex);
    }

    operatorDAO dao = new operatorDAOImpl(em);
    List<operator> operators = dao.findAll();
    for (operator op : operators) {
        System.out.print("Id: " + operator.getID());// <-- This doesnt work
        System.out.print(", First Name: " + operator.getName()); // <-- This doesnt work

    }
}
}

供参考,工作示例代码。警告:长

package nl.hu.ict;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

import nl.hu.ict.dao.HibernateUtil;
import nl.hu.ict.dao.LecturerDao;
import nl.hu.ict.dao.LecturerDaoHibImpl;
import nl.hu.ict.model.Lecturer;

public class MyHibernateTest {

private static EntityManagerFactory entityManagerFactory;

public static void main(String[] args) {
    // get the Hibernate - JPA entityManager
    EntityManager em = null;
    try {
        entityManagerFactory = HibernateUtil.getEntityManagerFactory();
        em = entityManagerFactory.createEntityManager();
    } catch (Throwable ex) {
        System.err.println("Failed to create sessionFactory object." + ex);
        throw new ExceptionInInitializerError(ex);
    }

    // get the program (not necessary)
    // MyHibernateTest test = new MyHibernateTest();

    LecturerDao lectDao = new LecturerDaoHibImpl(em);
    Integer id;

    // handle the database actions

    // list all lecturers
    List<Lecturer> lecturers = lectDao.findAll();
    for (Lecturer lecturer : lecturers) {
        System.out.print("Id: " + lecturer.getID());
        System.out.print(", First Name: " + lecturer.getFirstName());
        System.out.println(", Last Name: " + lecturer.getLastName());
        System.out.println(", Last Name: " + lecturer.getLastName());
    }

    // add an lecturer
    Lecturer lect = new Lecturer();
    lect.setFirstName("Bill");
    lect.setLastName("Pianoman");
    lectDao.persist(lect);
    // get the id of the added lecturer
    id = lect.getID();
    // list to view changes
    lecturers = lectDao.findAll();
    for (Lecturer lecturer : lecturers) {
        System.out.print(lecturer);
    }

    // change a lecturer
    // find find it again
    lect = lectDao.findById(id);
    // change it
    lect.setFirstName("Billy");
    lectDao.update(lect);
    // list to view changes
    lecturers = lectDao.findAll();
    for (Lecturer lecturer : lecturers) {
        System.out.print(lecturer);
    }

    // delete a lecturer
    // find it again
    lect = lectDao.findById(id);
    // delete it
    lectDao.delete(lect);
    // list to view changes
    lecturers = lectDao.findAll();
    for (Lecturer lecturer : lecturers) {
        System.out.print(lecturer);
    }
    em.close();
    System.out.println("-- einde --");
}
}

3 个答案:

答案 0 :(得分:2)

这是命名混乱;在这部分代码中:

operatorDAO dao = new operatorDAOImpl(em);
List<operator> operators = dao.findAll();
for (operator op : operators) {
    System.out.print("Id: " + operator.getID());// <-- This doesnt work
    System.out.print(", First Name: " + operator.getName()); // <-- This doesnt work
}

您创建一个名为运算符的局部变量,以引用从调用DAO的findAll方法返回的列表。在for循环体内,你有一个局部变量op。局部变量op的类型是运算符。但是你正在使用“运算符”,就好像它是变量的名称一样。将operator.getID()更改为op.getID()。编译器认为你在​​运算符类上调用了一个实例方法getID,这就是你得到“对非静态方法的静态引用”错误的原因。

您可以遵循命名约定来最小化这些类型的错误。类型(类和接口)应以大写字母开头。实例变量和局部变量应以小写字母开头。遵循惯例,此代码变为:

OperatorDAO dao = new OperatorDAOImpl(em);
List<Operator> operators = dao.findAll();
for (Operator operator : operators) {
    System.out.print("Id: " + operator.getID());
    System.out.print(", First Name: " + operator.getName());
}

答案 1 :(得分:0)

您可能希望使用op代替operator。后者是一个类名,你试图在该类上调用非静态的方法。

此外,java有非常好的约定,有助于快速识别此问题 - 类应以大写开头,而变量使用小写

答案 2 :(得分:0)

System.out.print("Id: " + operator.getID());// <-- This doesnt work
System.out.print(", First Name: " + operator.getName()); // <-- This doesnt work

应该是:

System.out.print("Id: " + op.getID());// <-- This doesnt work
System.out.print(", First Name: " + op.getName()); // <-- This doesnt work

当您需要实例化方法时,您尝试访问getID和getName作为静态方法。