anil
anil

Reputation: 143

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 ? 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

Upvotes: 1

Views: 13202

Answers (5)

Christian Kuetbach
Christian Kuetbach

Reputation: 16060

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;

Upvotes: 0

Cruncher
Cruncher

Reputation: 7812

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.

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.

Upvotes: 0

Logan Murphy
Logan Murphy

Reputation: 6230

why would you use static at all for the function? Use the this context.

 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);

Upvotes: 2

user2209008
user2209008

Reputation:

Pass the object you want to work with into the static function as an argument.

static void employee_salary_cal(emp employee, int t)

Upvotes: 0

Georgian
Georgian

Reputation: 8960

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.

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.

Upvotes: 5

Related Questions