1

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
5
  • Assuming you have to use a static method for this, pass an emp object to your employee_salary_cal method instead of just passing an int. Commented Sep 9, 2013 at 20:41
  • possible duplicate of how to access a non static member from a static method in java? Commented Sep 9, 2013 at 20:43
  • why is the function static to begin with? Commented Sep 9, 2013 at 20:44
  • @Simon this is not the same, note that the answer with most upvotes propose the usage of a Singleton which is not the right solution in this case. Commented Sep 9, 2013 at 20:45
  • no answer to you question, but please use the java code conventions: oracle.com/technetwork/java/javase/documentation/… Commented Sep 9, 2013 at 20:46

5 Answers 5

5

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Or easier, have a non-static employee_salary_cal(int t) method that handles this.
@LuiggiMendoze Well duh, but I'm willing to bet that this is a homework assignment, and he's required to make use of the static keyword.
Yes this appears to be the case, still the advice you explain in your answer doesn't explains that, duh
2

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

Comments

0

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

static void employee_salary_cal(emp employee, int t)

Comments

0

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.

Comments

0

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.