相关疑难解决方法(0)

如何修复 - 41:无法从静态上下文引用非静态变量 - >这是什么原因?

我正在尝试编写此代码以获取第一个initialCapacity素数,然后使用java按顺序打印它们.由于两个原因,它无法正常工作,首先我得到了错误

41:无法从静态上下文引用非静态变量listOfPrimeNumbers

当我尝试运行程序时,即使我将变量更改为静态并运行程序,它也只会打印出"1".所以它只是在构造函数Primes中迭代while循环一次,然后停止,无论我看起来多么努力,我都无法找到问题!有人能帮助我,即使你可以快速看看并告诉我可能出错的地方,我真的很感激.

另外,与非静态和静态变量和方法有关的故事是什么?使用这些时的最佳做法是什么?如果有人可以将我链接到描述这个的页面(我已经google了无效!)我很想读:)

非常感谢你们!

import java.util.*;
public class Primes {
  private ArrayList<Integer> listOfPrimeNumbers;

  public static void main(String[] args) {
    ArrayList<Integer> listOfPrimeNumbers;
    Primes generator=new Primes(50);
    print();
  }

  public Primes( int initialCapacity) {
    listOfPrimeNumbers = new ArrayList<Integer>(initialCapacity);    
    int index=0;
    int counter=0;
    while (counter != initialCapacity  ) {
      if (isPrime(index)) {
        listOfPrimeNumbers.add(index);
        counter++;
        System.out.println(counter);
        index++;
      }
      else {
        index++;
      }
    }
  }
  public boolean isPrime(int candidateNo) {
    Iterator<Integer> iter = listOfPrimeNumbers.iterator( );
    //in here ! ?
    int i=2;
    while ( iter.hasNext( ) …
Run Code Online (Sandbox Code Playgroud)

java primes static instance-variables

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

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

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

受保护和包访问

我试图了解受保护和包装的想法,我已经在编译器上尝试了它们,但它一直告诉我有问题

public class example{

    int s = example2.v;

    public static void main(String args[]){


    }
} 

public class example2 {

    int v = 0 ;

}
Run Code Online (Sandbox Code Playgroud)

谁能帮我这个?为什么它说:

无法从静态上下文引用非静态变量v.

变量''不是静态的!

java package

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

从其他类调用非静态方法

我正在完成我的任务,并遇到了一些错误.在一个班级我有这个方法:

public class Class1{
    public static boolean winningRecord(){
        final int WINNING_RECORD;
        return Class2.getPoints() > WINNING_RECORD;
    }
}

class Class2{
    int wins = 0;
    public int getPoints(){
        return wins; //More to it but to keep it simple I'll leave that out
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我收到错误"非静态方法无法从静态上下文引用...",所以我使getPoints()成为一个静态方法,使变量也是静态的,并且它可以工作.但是在打印对象的另一种方法中,它不起作用(我相信这是因为static关键字).

所以我的问题是,有一种方法可以在不创建第二个类的实例的情况下调用方法吗?这是我的一般想法代码,它应该让你了解正在发生的事情,如果没有,我会添加更多.

java

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

无法从静态上下文引用非静态字段

我有一个类作为静态的一些属性,类是静态的,但我得到的错误是字段不是静态的.

我无法找到解决方案,我看到的示例看起来非常相似.视图保持器始终在适配器内部作为静态公共静态

这是我的ArrayAdapter:

public class NoteAdapter extends ArrayAdapter<Note> {

public static class ViewHolder{
    TextView noteTitle;
    TextView noteBody;
    ImageView noteIcon;
}

public NoteAdapter(Context context, ArrayList<Note> notes) {
    super(context, 0, notes);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    Note note = getItem(position);
    ViewHolder viewHolder;

    if (convertView == null) {
        viewHolder = new ViewHolder();
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_row, parent, false);

        // == Here I am getting the error == 
        ViewHolder.noteTitle = (TextView) convertView.findViewById(R.id.text_item_note_title);
        ViewHolder.noteBody = (TextView) convertView.findViewById(R.id.text_item_note_body); …
Run Code Online (Sandbox Code Playgroud)

java android android-arrayadapter android-viewholder

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

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

我试图在我的main createFile方法中从OpenFile类中调用我的方法,但我不断收到错误,说我不能从静态上下文中调用非静态变量.

我确实尝试OpenFile of = new OpenFile();在我的main方法中调用,但是这没有用,所以我现在声明OpenFile我的main方法上面工作正常,但是每次我尝试使用OpenFiles方法之一时我都会得到相同的错误.

我尝试过使用一些东西,static但这只会导致我的IDE显示错误的sym类型错误,我认为这是由引起其他错误的任何原因引起的.

这是createFile来自OpenFile:

public class OpenFile {

    private Formatter file;

    public void createFile() throws FileNotFoundException{

        try{
            file = new Formatter("test.txt");
        } catch(Exception e) {
            System.out.println("Error creating file.");
    }
    }
Run Code Online (Sandbox Code Playgroud)

这是我的主要方法:

OpenFile of = new OpenFile();

public static void main(String[] args) {
    // TODO code application logic here

    of.createFile();
    intro();
    createAndShowRibbon();
    createAndShowNormalUI();

}
Run Code Online (Sandbox Code Playgroud)

它与Formatter有关吗?我以前从未使用过它.

谢谢.

java static non-static

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

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

我知道这个问题已经被问了很多次,而且我已经阅读了很多答案,但我无法理解如何解决我的问题.这是我的代码:

public class Circle
{
    public int diameter;
    public int xPosition;
    public int yPosition;
    public String color;


    public Circle()
    {
        diameter = 30;
        xPosition = 20;
        yPosition = 60;
        color = "blue";
    }

    public void toString()
    {
        System.out.println("The diameter of the circle is " + Circle.diameter);
        System.out.println("The x position of the circle is " + Circle.xPosition);
        System.out.println("The y position of the circle is " + Circle.yPosition);
        System.out.println("The colour of the circle is " + Circle.color);
    }


   public static void main(String[] …
Run Code Online (Sandbox Code Playgroud)

java

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

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

这是代码的一部分但是当我正在执行时我得到错误该程序是关于线性编程抱歉我的英语,或关于愚蠢的问题

"错误:无法从静态上下文引用非静态方法get_total_profit(String)

output = get_total_profit(ip1); 在输出^ 1错误"

import java.io.*;
import java.lang.*;
import java.util.*;
import java.text.DecimalFormat;

public class CandidateCode {

 String get_total_profit(String input1) {
    String[] inputs = input1.split(",");
    // Piece of farm land in square kilometer
    float L = Float.valueOf(inputs[0]);
    // Fertilizer in kg
    float F = Float.valueOf(inputs[1]);
    // Insecticide in kg
    float P = Float.valueOf(inputs[2]);
    // Fertilizer required in kg for square kilometer of Wheat
    float F1 = Float.valueOf(inputs[3]);
    // Insecticide required in kg for square kilometer of Wheat
    float …
Run Code Online (Sandbox Code Playgroud)

java string

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

Java自定义方法不起作用

我是初学者,我开始通过做一些练习来学习编程......为什么这个简单的java代码会给我一个错误?

class HelloWorldEdited {
    public int a = 5;
    public int b = 2;

    public static int sum() {
        return a + b;
    }

    public static void main(String[] args) {
        HelloWorldEdited obj = new HelloWorldEdited();

        System.out.println(obj.sum());
    }
}
Run Code Online (Sandbox Code Playgroud)

java

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