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

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

本文介绍了如何修复 - 41:无法从静态上下文引用非静态变量 - >这是什么原因?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写此代码以获取第一个initialCapacity素数,然后使用java按顺序打印它们。它有两个原因没有用,首先我得到错误

I'm trying to write this code to get the first initialCapacity prime numbers and then print them in sequence using java. It isn't working for two reasons, firstly I get the error

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

when I try to run the program, but even when I change the variable to static and run the program, it will only print out "1". So it is only iterating the while loop in the constructor Primes once, and then stopping and I simply cannot find the problem there no matter how hard I look ! Would anyone be able to help me out please, even if you could just give a very quick look and tell me what could be wrong, I'd really appreciate it.

此外,与非静态和静态变量和方法有关的故事是什么?使用这些时的最佳做法是什么?如果有人可以将我链接到描述这个的页面(我用Google搜索无效!)我很乐意阅读:)

Also, what is the story with relation to non-static and static variables and methods ? What is the best practice when using these ? If anyone could link me to a page describing this (I have googled to no avail!) I would love to read up :)

非常感谢你们!

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( ) ) {
      int next = iter.next( );
      if (next%i==0 && i!=1) {
        return false;
      }
    }
    return true;
  }

  public static void print( ) {
    int n = listOfPrimeNumbers.size();
    for(int i = 0; i <= n ; i++)
      System.out.println( listOfPrimeNumbers.get( i ) );

  }
}

我现在编辑了我的代码所以一切都是静态的(意味着我可以有多个实例?)。我现在有了这个,问题是它只打印出前51个数字,然后得到堆栈溢出,任何人都可以帮忙吗?谢谢:):

I have edited my code now so that everything is static (meaning I can have multiple instances?). I now have this, the problem being it just prints out the first 51 numbers, and then gets a stack overflow, can anyone help ? Thank you :) :

import java.util.*;
public class Primes {
  private static 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=2;
    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( );
    while ( iter.hasNext( ) ) {
      int next = iter.next( );
      if (next%candidateNo==0 && candidateNo!=1) {
        return false;
      }
    }
    return true;
  }

  public static void print( ) {
    int n = listOfPrimeNumbers.size();
    for(int i = 0; i <= n ; i++)
      System.out.println( listOfPrimeNumbers.get( i ) );

  }
}


推荐答案

listOfPrimeNumbers 是您班级的成员,这意味着 Primes 的每个实例都有自己的 listOfPrimeNumbers

listOfPrimeNumbers is a member of your class, which means that each instance of Primes has its own copy of listOfPrimeNumbers.

print 是一个静态函数,这意味着它与 Primes 的实例无关,因此,它无法访问任何 listOfPrimeNumbers 变量(每个类的实例一个)。

print is a static function, which means that it is not related to an instance of Primes, and as such, it doesn't have access to any of the listOfPrimeNumbers variables there are (one per instance of your class).

所以 listOfPrimeNumbers 必须是静态的(即整个世界只有一个副本),或者 print 不能是静态的。

So listOfPrimeNumbers would have to be static (i.e. there is only one copy in the whole wide world), or print can't be static.

这篇关于如何修复 - 41:无法从静态上下文引用非静态变量 - &gt;这是什么原因?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 21:31