Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2009
    Posts
    15
    Rep Power
    0

    Default 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

  2. #2
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    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

  3. #3
    makpandian's Avatar
    makpandian is offline Senior Member
    Join Date
    Dec 2008
    Location
    Chennai
    Posts
    441
    Rep Power
    6

    Default

    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)

  4. #4
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    i think you should create the class object
    if you change method to static, variable used also need to be changed to static

  5. #5
    ron87 is offline Member
    Join Date
    Mar 2009
    Posts
    52
    Rep Power
    0

    Default

    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.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Replies: 5
    Last Post: 03-21-2009, 09:32 PM
  2. Replies: 3
    Last Post: 03-20-2009, 12:35 AM
  3. Replies: 11
    Last Post: 11-19-2008, 03:13 AM
  4. Replies: 1
    Last Post: 08-07-2007, 05:05 AM
  5. Replies: 1
    Last Post: 08-01-2007, 09:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •