$ javac TestFilter.java
TestFilter.java:19: non-static variable this cannot be referenced from a static context
for(File f : file.listFiles(this.filterFiles)){
^
1 error
$ sed -i 's@this@TestFilter@g' TestFilter.java
$ javac TestFilter.java
$ java TestFilter
file1
file2
file3
Run Code Online (Sandbox Code Playgroud)
TestFilter.java
import java.io.*;
import java.util.*;
public class TestFilter {
private static final FileFilter filterFiles;
// STATIC!
static{
filterFiles = new FileFilter() {
// Not Static below. When static, an error:
// "accept(java.io.File) in cannot implement
// accept(java.io.File) in java.io.FileFilter;
// overriding method is static"
// …Run Code Online (Sandbox Code Playgroud) 我试图将mm/dd/yyyy格式化的日期解析为单独的字段,但是当我尝试编译时出现以下错误:
非静态方法next()不能从静态上下文中引用
可能导致错误的原因是什么?
import java.util.Scanner;
public class Problem39
{
public static void main(String [ ] args)
{
boolean isLeapYear =false;
int maxDay=0;
String stringDate;
System.out.println("Enter the date in mm/dd/yyyy format. "); //user input
Scanner keyboard = new Scanner(System.in); //read input
String date=Scanner.next(); //store input
String temp=date.split("/"); //parse date
int month=IntegerParseInt(temp[1]);
int day=IntegerParseInt(temp[0]);
int year=IntegerParseInt(temp[2]);
Run Code Online (Sandbox Code Playgroud) 我正在清除我对Java的概念.我对Java的了解远远不够,所以请耐心等待.
我试图理解静态方法和非静态方法intercalls.我知道 -
我的问题是关于非静态方法调用同一类的另一个非staic方法.在类声明中,当我们声明所有方法时,我们可以从非静态类中调用同一类的另一个非静态方法吗?
请举例说明.谢谢.
我正在阅读Kathy和Bert SCJP 1.6并遇到以下代码:
class Bar {
int barNum = 28;
}
class Foo {
Bar myBar = new Bar();
void changeIt(Bar myBar) {
myBar.barNum = 99;
System.out.println("myBar.barNum in changeIt is " + myBar.barNum);
myBar = new Bar();
myBar.barNum = 420;
System.out.println("myBar.barNum in changeIt is now " + myBar.barNum);
}
public static void main (String [] args) {
Foo f = new Foo();
System.out.println("f.myBar.barNum is " + f.myBar.barNum);
f.changeIt(f.myBar);
System.out.println("f.myBar.barNum after changeIt is "
+ f.myBar.barNum);
}
}
Run Code Online (Sandbox Code Playgroud)
虽然它是阴影变量的主题,但我无法理解如何在main()方法(静态)中引用非静态变量myBar?
在c ++中,静态或非静态变量保留在哪里?我的意思是在记忆中.
并且,什么时候初始化静态或非静态变量?
需要有人帮助我明白我的想法.谢谢!
那C呢?相同?
我有以下代码:
#include <fstream>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
struct node{
vector<int> vic;
bool visitato = false;
};
int main (){
vector<node> grafo;
ifstream in("input.txt");
int n, m, s, from, to;
in >> n >> m >> s;
grafo.resize(n);
for (int i = 0; i < m; i++){
in >> from >> to;
grafo[from].vic.push_back(to);
}
for (int i = 0; i < grafo.size(); i++)
for(int j = 0; j < grafo[i].vic.size(); j++)
cout << "From node " …Run Code Online (Sandbox Code Playgroud) 我的小组正在尝试创建一个联系人列表,其中也可以添加联系人,但是他们收到以下错误:非静态方法populatelist()无法通过静态上下文引用.但是我们不知道这是否真的有效.任何帮助表示赞赏
public class Profile extends Activity {
ImageView contactImageImgView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.profile);
Button editProfileButton = (Button) findViewById(R.id.BeditProfile);
Button addContactButton = (Button) findViewById(R.id.AdContactButton);
//Text ChangeProfilePictureText = (Text) findViewById(R.id.ChangeProfilePicture);
String Name = getIntent().getStringExtra("Name");
String eMail = getIntent().getStringExtra("Mail");
String Mobile = getIntent().getStringExtra("Mobile");
String Adress = getIntent().getStringExtra("Adress");
String Bio = getIntent().getStringExtra("Bio");
contactImageImgView = (ImageView) findViewById(R.id.imgViewContactImage);
TextView tv_Name = (TextView) findViewById(R.id.Name);
TextView tv_Mail = (TextView) findViewById(R.id.Email);
TextView tv_Mobile = (TextView) findViewById(R.id.Mobile);
TextView tv_Adress = (TextView) findViewById(R.id.Adress);
TextView tv_Bio = (TextView) findViewById(R.id.Bio); …Run Code Online (Sandbox Code Playgroud) 为什么下面的代码片段使用非静态来运行程序???以这种方式运行程序有好处吗???
public static void main(String[] args) {
Main go = new Main();
go.start();
}
public Main() {
}
public void start() {
//SOME CODE HERE
}
Run Code Online (Sandbox Code Playgroud) 我正在此线程上阅读有关C中的静态函数的信息:https : //stackoverflow.com/a/558201/7997108
据我了解,基本上,您在其中定义静态函数的地方是可以调用它的唯一“位置” /文件(即fileA.c即文件),这一定会使该函数对那个.c或.h文件(或翻译单位)。但是,如果您将此文件#include到其他文件(fileB.c)中,您仍然可以在那里使用它?
因此,我试图了解在哪种情况下您希望函数对自己的.c保持静态,以及如果仅通过在其定义的文件中仍然可以使用该“私有” /静态函数,该函数的意义如何。
另外,据我了解,如果您不包括定义了某些功能的其他文件,则无论如何您将无法使用/调用该功能,对吗?
换句话说,我无法理解静态函数的典型用例是什么,以及它与非静态函数的基本区别。
如何显示抽象类 Confused("ConfusedValue") 中“name”非静态字段的值?我尝试了3种方法,但都有错误。我只能显示 Confuslable 的名称值(“ConfusableValue”)
interface Confusable {
String name = "ConfusableValue";
String confuse();
}
abstract class Confused {
String name = "ConfusedValue";
abstract Object confuse();
}
public class Test extends Confused implements Confusable {
public static void main(String[] args) {
Test a = new Test();
// --- OK
System.out.println("Confusable.name: " + Confusable.name);
// --- Errors
System.out.println("name: " + name); // Error : The field name is ambiguous
System.out.println("Confused.name: " + Confused.name); // Error : Cannot make a static reference …Run Code Online (Sandbox Code Playgroud)