相关疑难解决方法(0)

"非静态方法无法从静态上下文中引用"背后的原因是什么?

非常常见的初学者错误是当您尝试"静态"使用类属性而不创建该类的实例时.它会留下您提到的错误消息:

您可以将非静态方法设为静态,也可以使该类的实例使用其属性.

为什么?我不是要求解决方案.我很高兴知道它背后的原因是什么.核心原因!

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)

java static

259
推荐指数
7
解决办法
61万
查看次数

"无法从静态上下文引用非静态方法"错误

我有一个名为的类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)不能从静态上下文引用

我只是试图onloanMedia类中的变量更改为"是" GUI.

我看过其他主题有相同的错误消息,但没有点击!

java static compiler-errors

69
推荐指数
2
解决办法
34万
查看次数

this:不能在静态上下文中使用它

你能帮我解决下面的代码吗?错误是:"无法在静态上下文中使用它"

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)

java this

20
推荐指数
2
解决办法
7万
查看次数

无法从静态上下文引用非静态变量名称

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)

当我运行此代码时,我发现错误,表示无法从静态上下文引用非静态变量名称

java

10
推荐指数
1
解决办法
6万
查看次数

非静态变量,不能从静态上下文引用

错误来自此行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)

java static non-static

8
推荐指数
1
解决办法
3万
查看次数

无法从静态context-Main方法引用非静态字段

我的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)

我怎么解决这个问题?

java methods static program-entry-point class

7
推荐指数
1
解决办法
2万
查看次数

为什么不能从静态方法调用非静态方法?

我的类中有一个静态方法[Method1],它在同一个类中调用另一个方法[Method2],而不是静态方法.但这是禁忌.我收到此错误:

非静态字段,方法或属性"ClassName.MethodName()"需要对象引用

有人可以简单描述一下原因吗?包括可能与此相关的其他事情.

编辑:谢谢你的回复,伙计们!

这是我的一个小错误,让我试着解释一下.该类是工厂的一部分,工厂有所有类的实例.但是这个方法是NoWhere,因为它不是该类实现的接口的一部分,而是仅作为辅助函数添加.

答案也很有帮助!

c# static-methods class

5
推荐指数
1
解决办法
7355
查看次数

如何避免“对构造函数中的非最终静态字段进行可能的不安全赋值”(AssignmentToNonFinalStatic)

我的代码目前受到“对构造函数中非最终静态字段的可能不安全赋值”(PMD 中的 AssignmentToNonFinalStatic)的影响。

类写成单例类,受此警告影响的属性如下所示

私有静态字符串 myProperty;

并由这个结构填充:

public SystemPropertyUtils() throws ConfigException {
    someMethodThrowingConfigException();
    myProperty = "someValue" + this.someOtherValueFromAThreadSafeString;
}
Run Code Online (Sandbox Code Playgroud)

有没有详细的方法来否定这个警告?

java pmd

2
推荐指数
1
解决办法
4281
查看次数

非静态方法不能从静态上下文中引用

下面的代码出现在我正在尝试创建的包的主类中.它从名为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 oop static

1
推荐指数
1
解决办法
9838
查看次数

为什么非静态变量不能从静态上下文引用 - reg

可能重复:
无法从静态上下文引用非静态变量(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在另一个文件中有相同的类,并且我做同样的事情,它不会给我一个错误.

即使在这种情况下,我们也试图从静态方法访问非静态类.但这并没有给出任何错误.为什么?

java class

1
推荐指数
2
解决办法
7026
查看次数