Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > newbie Java question - non-static method cannot be referenced from a static context

Reply
Thread Tools

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

 
 
G. Burton
Guest
Posts: n/a
 
      02-20-2004
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!
 
Reply With Quote
 
 
 
 
Barry White
Guest
Posts: n/a
 
      02-20-2004
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!


 
Reply With Quote
 
 
 
 
Sudsy
Guest
Posts: n/a
 
      02-21-2004
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java beginner - non static method cannot be referenced from a static context javnewbie General Computer Support 0 01-02-2010 09:40 AM
Re: non-static method cannot be referenced from a static context Johannes Beekhuizen Java 0 08-05-2005 05:33 PM
non-static method cannot be referenced from a static context Johannes Beekhuizen Java 14 08-05-2005 04:48 PM
Question: non-static method dummy(int) cannot be referenced from a static context Michael Java 3 01-27-2004 08:45 PM
Please help: Non-static method wait(long) cannot be referenced from static context Gary Java 1 01-16-2004 11:35 PM



Advertisments