非常常见的初学者错误是当您尝试"静态"使用类属性而不创建该类的实例时.它会留下您提到的错误消息:
您可以将非静态方法设为静态,也可以使该类的实例使用其属性.
为什么?我不是要求解决方案.我很高兴知道它背后的原因是什么.核心原因!
private java.util.List<String> someMethod(){
/* Some Code */
return someList;
}
public static void main(String[] strArgs){
// The following statement causes the error. You know why..
java.util.List<String> someList = someMethod();
}
Run Code Online (Sandbox Code Playgroud) 我有一个名为的类Media,它有一个名为的方法setLoanItem:
public void setLoanItem(String loan) {
this.onloan = loan;
}
Run Code Online (Sandbox Code Playgroud)
我试图从以GUI下列方式命名的类中调用此方法:
public void loanItem() {
Media.setLoanItem("Yes");
}
Run Code Online (Sandbox Code Playgroud)
但是我收到了错误
非静态方法setLoanItem(java.lang.String)不能从静态上下文引用
我只是试图onloan将Media类中的变量更改为"是" GUI.
我看过其他主题有相同的错误消息,但没有点击!
你能帮我解决下面的代码吗?错误是:"无法在静态上下文中使用它"
public class Sample2 {
/**
* @param args
*/
public static void main(String[] args)
{
Sample2 sam=new Sample2();
//Below code works fine
System.out.println(sam);
//Below code is displaying error
System.out.println(this);
}
}
Run Code Online (Sandbox Code Playgroud) class Singer
{
String name;
String album;
public Singer(){
name="Whitney Houson";
album="Latest Releases";
}
public static void main(String[] args)
{
System.out.println("Name of the singer is "+name);
System.out.println("Album Information stored for "+album);
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行此代码时,我发现错误,表示无法从静态上下文引用非静态变量名称
错误来自此行BoardState addme = new BoardState();
由于某种原因,它所指向的非静态变量是"新的".我不清楚如何修复此错误,因为新的并不意味着变量,而不是.
通过stackoverflow记录查看此错误通常来自非静态方法,该方法通常通过使方法静态或完全绕过该方法来解决.Ť
下面的代码用于引用此语句之前和之后发生的事情.
public class IntelligentTicTacToe extends TicTacToe {
public class BoardState{
public String TTTState;
public int[][] defensiveOppsArray;
public int[][] offensiveOppsArray;
public String str;
public int cnt;
}
public static ArrayList<BoardState> memory = new ArrayList<BoardState>();
public static boolean makeMove(){
char[] oArray = new char[TicTacToeArray.length];
int[][] defensiveOppsArray = new int[TicTacToeArray.length][TicTacToeArray.length];
int[][] offensiveOppsArray = new int[TicTacToeArray.length][TicTacToeArray.length];
int[][] sumOppsArray = new int[TicTacToeArray.length][TicTacToeArray.length];
//converts our Array into a String
String x = convertTTTArrayToString();
//Goes through the conditions to see …Run Code Online (Sandbox Code Playgroud) 我的Spring-Boot应用程序中有2个类:
-任务
-Runner
runner类包含我的main方法,我尝试从Tasks类中调用一个方法:
亚军:
@Component
public class Runner {
Tasks tasks;
@Autowired
public void setTasks(Tasks tasks){
this.tasks=tasks;
}
public static void main(String[] args){
//error being caused by below line
tasks.createTaskList();
}
Run Code Online (Sandbox Code Playgroud)
任务类:
@Service
public class Tasks {
public void createTaskList() {
//my code
}
//other methods
}
Run Code Online (Sandbox Code Playgroud)
在我的Runner中,当我尝试在Tasks类中调用createTaskList()方法时,我收到以下错误:
Non static field 'tasks' cannot be referenced from a static context
Run Code Online (Sandbox Code Playgroud)
我怎么解决这个问题?
我的类中有一个静态方法[Method1],它在同一个类中调用另一个方法[Method2],而不是静态方法.但这是禁忌.我收到此错误:
非静态字段,方法或属性"ClassName.MethodName()"需要对象引用
有人可以简单描述一下原因吗?包括可能与此相关的其他事情.
编辑:谢谢你的回复,伙计们!
这是我的一个小错误,让我试着解释一下.该类是工厂的一部分,工厂有所有类的实例.但是这个方法是NoWhere,因为它不是该类实现的接口的一部分,而是仅作为辅助函数添加.
答案也很有帮助!
我的代码目前受到“对构造函数中非最终静态字段的可能不安全赋值”(PMD 中的 AssignmentToNonFinalStatic)的影响。
类写成单例类,受此警告影响的属性如下所示
私有静态字符串 myProperty;
并由这个结构填充:
public SystemPropertyUtils() throws ConfigException {
someMethodThrowingConfigException();
myProperty = "someValue" + this.someOtherValueFromAThreadSafeString;
}
Run Code Online (Sandbox Code Playgroud)
有没有详细的方法来否定这个警告?
下面的代码出现在我正在尝试创建的包的主类中.它从名为Journey的帮助程序类引用对象和方法.在journeyCost星号标记的方法中调用方法时,我得到"非静态方法不能从静态上下文引用"错误.这使我感到困惑,因为我认为在第二行创建的Journey对象"thisJourney"构成了类的实例,因此意味着上下文不是静态的.谢谢,Seany.
public boolean journey(int date, int time, int busNumber, int journeyType){
Journey thisJourney = new Journey(date, time, busNumber, journeyType);
if (thisJourney.isInSequence(date, time) == false)
{
return false;
}
else
{
Journey.updateCurrentCharges(date);
thisJourney.costOfJourney = Journey.journeyCost(thisJourney);*****
Journey.dayCharge = Journey.dayCharge + thisJourney.costOfJourney;
Journey.weekCharge = Journey.weekCharge + thisJourney.costOfJourney;
Journey.monthCharge = Journey.monthCharge + thisJourney.costOfJourney;
Balance = Balance - thisJourney.costOfJourney;
jArray.add(thisJourney);
}
}
Run Code Online (Sandbox Code Playgroud) 可能重复:
无法从静态上下文引用非静态变量(java)
public class DemoJava {
public class Hello {
void fun()
{
System.out.println("This is static fun man!!");
}
}
public static void main(String[] args) {
Hello hello = new Hello();
hello.fun();
}
}
Run Code Online (Sandbox Code Playgroud)
在这个例子中,它会给我一个错误,因为我试图从静态方法访问非静态类.精细.例如,如果我Hello在另一个文件中有相同的类,并且我做同样的事情,它不会给我一个错误.
即使在这种情况下,我们也试图从静态方法访问非静态类.但这并没有给出任何错误.为什么?