1 Replies - 689 Views - Last Post: 20 February 2011 - 08:38 PM Rate Topic: -----

#1 xterminal01  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 83
  • Joined: 18-September 10

non-static variable can not be referenced from a static context error

Posted 20 February 2011 - 08:22 PM

I am trying to make a program that inputs a personís birth date << month - day - year >> and todayss date (<< month - day - year >> ). Then call method calc() to determine how many years old the person is. The output will be the number of years and the display...
Keep getting the non-static variable can not be referenced from a static context error ?
This is the main
package age_verification;

public class Main {
    int month, day;
        int year = 0;


    public static void main(String[] args) {


        calculate_age CalculateAge = new calculate_age();

        Scanner user_input = new Scanner(System.in);

        System.out.print("Please enter ");
                month = user_input.nextInt();
        System.out.print("Plese enter the day the person was born: ");
                day = user_input.nextInt();
        System.out.print("Please enter the Year the person was born: ");
                year = user_input.nextInt();

                CalculateAge.calculateAge(day);

    }

}




And this is the Method
/*
package age_verification;
public class calculate_age {
     Main calculation = new Main();

     public void calculateAge(int age) {
          age = 2011 - calculation.year;
          System.out.printf("The person was born %d/%d/%d and is %d years old! ", calculation.month, calculation.day, calculation.year, age);

    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: non-static variable can not be referenced from a static context error

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: non-static variable can not be referenced from a static context error

Posted 20 February 2011 - 08:38 PM

should be

CalculatedAge.month = user_input.nextInt();
CalculatedAge.day = user_input.nextInt();

and so on

Your main() method is just to instantiate a CalculatedAge object and the to set/retreive its instance variables
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1