Results 1 to 6 of 6
- 05-03-2009, 12:14 AM #1
Member
- Join Date
- Mar 2009
- Posts
- 15
- Rep Power
- 0
non-static method cannot be referenced from a static context.
I have the line of code in a class X:
{code}Database.addIngenieur(new Ingenieur(naam,voornaam,paswoord,ranking,email,tel efoon));{code}
In my Database class I have the method addIngenieur:
{code}public void addIngenieur(Ingenieur deIngenieur)
{
ingenieurs.add(deIngenieur);
}{code}
which should add the ingenieur to the arraylist ingenieurs.
My class Ingenieur looks like the following:
{code}
public class Ingenieur
{
private String naam;
private String voornaam;
private String ranking;
private String email;
private String telefoonnummer;
private String nota;
private String paswoord;
public Ingenieur(String deNaam, String deVoornaam, String hetPaswoord, String deRanking, String deEmail, String hetTelefoonnummer)
{
naam = deNaam;
voornaam = deVoornaam;
ranking = deRanking;
email = deEmail;
telefoonnummer = hetTelefoonnummer;
paswoord = hetPaswoord;
nota = "";
}
//Some other methods below...
}
{code}
But it gives me the following error in class X: non-static method addIngenieur(Ingenieur) cannot be referenced from a static context.
I just don´t see the error which should be obvious to a real programmer.
Can anyone help me?
Best Regards
- 05-03-2009, 12:43 AM #2
addIngenieur is not a static method, yet you are trying to call it on it's class, rather than an instance of the class.
Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
- 05-06-2009, 10:49 AM #3
You can access only static methods with out objects.Hence if you want to do so,Change the method in static mode.
Mak
(Living @ Virtual World)
- 05-06-2009, 11:11 AM #4
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 5
i think you should create the class object
if you change method to static, variable used also need to be changed to static
- 05-06-2009, 12:24 PM #5
Member
- Join Date
- Mar 2009
- Posts
- 52
- Rep Power
- 0
if a method is not static the u have to call it using object of class.
if u directly want to call a method using class name then declare it as static.
You can not call non static method from static context.
- 05-07-2009, 04:05 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Just go through the following page, it may help you to have a better idea about the static members in Java.
The Essence of OOP using Java: Static Members
Similar Threads
-
non-static method add(double,double) cannot be referenced from a static context
By cravi85 in forum Java SoftwareReplies: 5Last Post: 03-21-2009, 09:32 PM -
non-static member can not be referenced from a static context
By christina in forum New To JavaReplies: 3Last Post: 03-20-2009, 12:35 AM -
Method cannot be referenced from a static context - HELP
By jmorris in forum New To JavaReplies: 11Last Post: 11-19-2008, 03:13 AM -
Error: Non-static method append(char) cannot be referenced from a static context
By paul in forum Advanced JavaReplies: 1Last Post: 08-07-2007, 05:05 AM -
Error: non-static variable height cannot be referenced from a static context at line
By fernando in forum AWT / SwingReplies: 1Last Post: 08-01-2007, 09:25 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks