简体   繁体   English

在Java中以静态方法访问非静态数据

[英]access non static data in static method in java

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 ? 我是一名新的Java学习者,我正在制作一个程序,希望以静态方法访问非静态数据成员的值,但规则说我不能这样做,但我们可以在创建对象后访问它,我的问题是那如果我做那个类的对象,那个数据成员的旧值就被抹掉了,为什么? how can i use it old value of a not static data member in a static method 如何在静态方法中使用非静态数据成员的旧值

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

Just pass your emp object to the static method, instead of creating a new instance of it. 只需将您的emp对象传递给静态方法,而不是为其创建新实例。 The "rule" is that you can't access instance variables and methods, but a static method can receive external objects and fiddle with them. “规则”是您不能访问实例变量和方法,但是静态方法可以接收外部对象并对其进行摆弄。

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. 我建议您再次遵循一些非常基础的教程。

why would you use static at all for the function? 您为什么要对函数全部使用static? Use the this context. 使用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);

将您要使用的对象作为参数传递给静态函数。

static void employee_salary_cal(emp employee, int t)

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. 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. 就像其他陈述一样,如果要从静态访问实例,则必须通过将实例传递给静态方法来告知该实例。

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只创建一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM