newbie Java question - non-static method cannot be referenced from a static context

Discussion in 'Java' started by G. Burton, Feb 20, 2004.

  1. G. Burton

    G. Burton Guest

    This is probably painfully obvious to a real programmer... Attempts at
    compiling the following program result in the message:
    Employee.java:9: non-static method
    initEmployee(java.lang.String,float,boolean)
    cannot be referenced from a static context
    initEmployee(FRED, 6012f, true);
    ^
    1 error

    This is the entire source java file:
    public class Employee
    {

    public static void main(String[] args)
    {
    // final indicates a constant.
    final String FRED = "Fred";
    Employee emp = new Employee();
    initEmployee(FRED, 6012f, true);
    String currentName = emp.displayEmpName();
    System.out.println(currentName);
    }

    public void initEmployee(String employeeName_In, float sal_In,
    boolean activeInd_In)
    { name = employeeName_In;
    salary = sal_In;
    active_Ind = activeInd_In;
    System.out.println("Name: "+name);
    System.out.println("Salary: "+salary);
    System.out.println("Active Indicator: "+active_Ind);
    System.out.println("Init Done");
    }

    public String displayEmpName()
    { return name; }

    private String name;
    private float salary;
    private boolean active_Ind;
    }

    Any help to get a beginner going would be greatly appreciated.

    Thanks in advance!
     
    G. Burton, Feb 20, 2004
    #1
    1. Advertising

  2. G. Burton

    Barry White Guest

    Re: newbie Java question - non-static method cannot be referencedfrom a static context

    Hi,

    initEmployee is an instance method (it acts on a specific instance of
    the class) but you are calling it from a static context (the static main
    method). If you just call initEmployee which instance are you referring
    to (none). You need to call emp.initEmployee(...).

    It's perfectly fine having the main method in a calss such as Employee
    but I think if you are learning java it confuses things. Perhaps
    something like:

    public class TestEmployee
    {
    public static void main(String[] args)
    {
    Employee emp = new Employee();
    emp.initEmployee("Fred", 6012f, true);
    }
    }

    public class Emplyee
    { // etc

    Regards,
    Barry

    G. Burton wrote:
    > This is probably painfully obvious to a real programmer... Attempts at
    > compiling the following program result in the message:
    > Employee.java:9: non-static method
    > initEmployee(java.lang.String,float,boolean)
    > cannot be referenced from a static context
    > initEmployee(FRED, 6012f, true);
    > ^
    > 1 error
    >
    > This is the entire source java file:
    > public class Employee
    > {
    >
    > public static void main(String[] args)
    > {
    > // final indicates a constant.
    > final String FRED = "Fred";
    > Employee emp = new Employee();
    > initEmployee(FRED, 6012f, true);
    > String currentName = emp.displayEmpName();
    > System.out.println(currentName);
    > }
    >
    > public void initEmployee(String employeeName_In, float sal_In,
    > boolean activeInd_In)
    > { name = employeeName_In;
    > salary = sal_In;
    > active_Ind = activeInd_In;
    > System.out.println("Name: "+name);
    > System.out.println("Salary: "+salary);
    > System.out.println("Active Indicator: "+active_Ind);
    > System.out.println("Init Done");
    > }
    >
    > public String displayEmpName()
    > { return name; }
    >
    > private String name;
    > private float salary;
    > private boolean active_Ind;
    > }
    >
    > Any help to get a beginner going would be greatly appreciated.
    >
    > Thanks in advance!
     
    Barry White, Feb 20, 2004
    #2
    1. Advertising

  3. G. Burton

    Sudsy Guest

    Re: newbie Java question - non-static method cannot be referencedfrom a static context

    G. Burton wrote:
    <snip>
    > public static void main(String[] args)
    > {
    > // final indicates a constant.
    > final String FRED = "Fred";
    > Employee emp = new Employee();
    > initEmployee(FRED, 6012f, true);


    emp.initEmployee(FRED, 6012f, true);

    > String currentName = emp.displayEmpName();
    > System.out.println(currentName);
    > }

    <snip>
    > Any help to get a beginner going would be greatly appreciated.
    >
    > Thanks in advance!


    HTH
     
    Sudsy, Feb 21, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gary
    Replies:
    1
    Views:
    3,994
    Ryan Stewart
    Jan 16, 2004
  2. Michael
    Replies:
    3
    Views:
    4,945
    Adam Jenkins
    Jan 27, 2004
  3. Johannes Beekhuizen
    Replies:
    14
    Views:
    1,343
    John B. Matthews
    Aug 5, 2005
  4. Johannes Beekhuizen
    Replies:
    0
    Views:
    1,540
    Johannes Beekhuizen
    Aug 5, 2005
  5. Johannes Beekhuizen
    Replies:
    1
    Views:
    541
    Thomas Fritsch
    Aug 5, 2005
Loading...

Share This Page