我有以下代码:
public class UserRepository
{
private MyDataSource myDataSource = new MyDataSource();
public static User CreateUser( int id, String firstName, String lastName )
{
myDataSource.propertyOfThis...
// myDataSource is not accessible and yet i have declared it as a property of UserRespository?
}
...
我在这里缺少什么?
最佳答案
myDataSource
不是静态
private MyDataSource myDataSource = new MyDataSource();
但是CreateUser
是静态
public static User CreateUser( int id, String firstName, String lastName )
因此,将 myDataSource
设置为 static
或删除 CreateUser
的 static
修饰符。
哦,或者一个new UserRepository()
。
关于java - 从类方法内访问类属性 - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17426434/