Java-无法对非静态字段进行静态引用
Java-Cannot make a static reference to the non-static field
好的,所以我在多年后重新访问 java,当我发现我在以下代码段中有错误时,我只是在尝试一些随机程序。有人可以告诉我如何解决这个问题吗?我知道静态方法不能访问非静态变量,但我为它创建了一个实例,对吗?另外,我对阅读其他一些问题一无所知,所以请尝试帮助我。
import java.io.*;
public class phone
{
int x=6;
int getx()//I also tried using this function but everything in vain
{
return x;
}
}
public class Testing_inheritance extends phone
{
public static void main (String args[])throws IOException
{
phone xy=new phone();
int y=phone.x;
y+=10;
System.out.println("The value of x is " +y);
}
}
您可能打算访问您创建的实例的实例变量:
phone xy = new phone();
int y = xy.x;
因为 x
不是静态变量,所以如果不指定 phone
class.
的实例就无法访问它
当然这也会失败,除非您将 x
的访问级别更改为 public
(这是可能的但不可取 - 您应该使用 getter 和 setter 方法,而不是直接从 class).
外部操作实例变量
x
不是 static
。您需要通过对象引用来访问它。
做
int y = xy.getx(); //could do xy.x, but better to access through method
此外,最好坚持 Java 命名约定
几乎完成了,我做了一些小但非常重要的更改,我希望你能明白,否则请问 ;-)
Phone.java
public class Phone //<--- class with capital letter always
{
int x=6;
int getx()//I also tried using this function but everything in vain
{
return x;
}
}
Testing_inheritance.java
import java.io.*;
public class Testing_inheritance extends Phone
{
public static void main (String args[])throws IOException
{
Phone xy=new Phone();
int y= xy.getx(); //<--- principle of encapsulation
y+=10;
System.out.println("The value of x is " +y);
}
}
OR 私有内部 class :
import java.io.IOException;
public class Phone {
int x = 6;
int getx()// I also tried using this function but everything in vain
{
return x;
}
private static class Testing_inheritance extends Phone {
public static void main(String args[]) throws IOException {
Phone xy = new Phone();
int y = xy.getx();
y += 10;
System.out.println("The value of x is " + y);
}
}
}
好的,所以我在多年后重新访问 java,当我发现我在以下代码段中有错误时,我只是在尝试一些随机程序。有人可以告诉我如何解决这个问题吗?我知道静态方法不能访问非静态变量,但我为它创建了一个实例,对吗?另外,我对阅读其他一些问题一无所知,所以请尝试帮助我。
import java.io.*;
public class phone
{
int x=6;
int getx()//I also tried using this function but everything in vain
{
return x;
}
}
public class Testing_inheritance extends phone
{
public static void main (String args[])throws IOException
{
phone xy=new phone();
int y=phone.x;
y+=10;
System.out.println("The value of x is " +y);
}
}
您可能打算访问您创建的实例的实例变量:
phone xy = new phone();
int y = xy.x;
因为 x
不是静态变量,所以如果不指定 phone
class.
当然这也会失败,除非您将 x
的访问级别更改为 public
(这是可能的但不可取 - 您应该使用 getter 和 setter 方法,而不是直接从 class).
x
不是 static
。您需要通过对象引用来访问它。
做
int y = xy.getx(); //could do xy.x, but better to access through method
此外,最好坚持 Java 命名约定
几乎完成了,我做了一些小但非常重要的更改,我希望你能明白,否则请问 ;-)
Phone.java
public class Phone //<--- class with capital letter always
{
int x=6;
int getx()//I also tried using this function but everything in vain
{
return x;
}
}
Testing_inheritance.java
import java.io.*;
public class Testing_inheritance extends Phone
{
public static void main (String args[])throws IOException
{
Phone xy=new Phone();
int y= xy.getx(); //<--- principle of encapsulation
y+=10;
System.out.println("The value of x is " +y);
}
}
OR 私有内部 class :
import java.io.IOException;
public class Phone {
int x = 6;
int getx()// I also tried using this function but everything in vain
{
return x;
}
private static class Testing_inheritance extends Phone {
public static void main(String args[]) throws IOException {
Phone xy = new Phone();
int y = xy.getx();
y += 10;
System.out.println("The value of x is " + y);
}
}
}