我有以下代码:
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
不是static
private MyDataSource myDataSource = new MyDataSource();
但是
CreateUser
是static
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/