在java中的静态方法中访问非静态数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18706655/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
access non static data in static method in java
提问by anil
i am a new java learner and i am making a program where i want to access the value of a not static data member in a static method but rule says that i cant not do this but we can access it after creating a object my question is that if i make a object of that class that old value of that data member is erase why ? how can i use it old value of a not static data member in a static method
我是一个新的 Java 学习者,我正在制作一个程序,我想在一个静态方法中访问一个非静态数据成员的值,但规则说我不能这样做,但我们可以在创建一个对象后访问它我的问题是如果我创建该类的对象,该数据成员的旧值是擦除为什么?如何在静态方法中使用非静态数据成员的旧值
import java.util.Scanner;
class emp
{
String name;
int rate;
static String c_name="TCS";
void setdata(String n,int s)
{
name=n;
rate=s;
}
static void employee_salary_cal(int t)
{
int day,rate1,Total;
day=t;
emp e2=new emp();
rate1=e2.rate;
Total=rate1*day;
System.out.println("Total salary " +Total);
}
void showData()
{
System.out.println("Employee name = " +name);
System.out.println("Employee pay rate per day = " +rate);
}
}
class emp_main
{
public static void main(String args[])
{
int da;
emp e1=new emp();
e1.setdata("alex",100);
System.out.println("Company name = " +emp.c_name);
e1.showData();
System.out.println("Enter Work days in months ");
Scanner sc=new Scanner(System.in);
da=sc.nextInt();
emp.employee_salary_cal(da);
}
}
program output :
程序输出:
Company name = TCS
Employee name = alex
Employee pay rate per day = 100
Enter Work days in months
25
Total salary 0
回答by GGrec
Just pass your emp
object to the static method, instead of creating a new instance of it. The "rule" is that you can't access instance variables and methods, but a static method can receive external objects and fiddle with them.
只需将您的emp
对象传递给静态方法,而不是创建它的新实例。“规则”是你不能访问实例变量和方法,但静态方法可以接收外部对象并摆弄它们。
static void employee_salary_cal(emp e, int t)
{
System.out.println("Total salary " + e.rate * t);
}
On another note, you are lacking serious programming fundamentals. I recommend you follow some really basic tutorials, again.
另一方面,您缺乏认真的编程基础。我建议您再次学习一些非常基本的教程。
回答by Colin Basnett
Pass the object you want to work with into the static function as an argument.
将您要使用的对象作为参数传递到静态函数中。
static void employee_salary_cal(emp employee, int t)
回答by Logan Murphy
why would you use static at all for the function? Use the this
context.
你为什么要为这个函数使用 static 呢?使用this
上下文。
void employee_salary_cal(int day)
{
System.out.println("Total salary " + (this.rate * day));
}
Then you can just call it as an instance function like so
然后你可以像这样调用它作为一个实例函数
emp e = new emp();
e.employee_salary_cal(5);
回答by Cruncher
It wouldn't make sense to be able to access an instance variable from a static context.
能够从静态上下文访问实例变量是没有意义的。
For each class there is exactly 1 set of static fields. However, each class could have any number of copies of each of its instance variables. The JVM would have no idea which copy of the instance variable you were talking about.
对于每个类,恰好有 1 组静态字段。但是,每个类都可以拥有其每个实例变量的任意数量的副本。JVM 不知道您正在谈论的实例变量的哪个副本。
Like other have stated, if you want to access an instance from static, you have to tell the static method which instance, by passing the instance into it.
就像其他人所说的那样,如果你想从静态访问一个实例,你必须通过将实例传递给静态方法来告诉静态方法是哪个实例。
回答by Christian Kuetbach
The difference between static and non-static:
静态和非静态的区别:
class MyClass {
public int nonStatic=1;
public static int isStatic = 2;
}
MyClass a=new MyClass();
a.nonStatic = 3 // nonStatic =3; isStatic=2
MyClass b=new MyClass(); // nonStatic =1; isStatic=2
MyClass.isStatic = 3; // nonStatic =1; isStatic=3
MyClass c=new MyClass(); // nonStatic =1; isStatic=3
What I want to explain is, If you create N instances of MyClass, you also create N values (or more precise int-pointer in memory) for nonStatic, but only one for isStatic;
我想解释的是,如果你创建了 MyClass 的 N 个实例,你也会为 nonStatic 创建 N 个值(或更精确的内存中的 int 指针),但对于 isStatic 只创建一个;