Results 1 to 6 of 6
  1. #1
    cravi85 is offline Member
    Join Date
    Mar 2009
    Posts
    2
    Rep Power
    0

    Question non-static method add(double,double) cannot be referenced from a static context

    while working the RMI program, i saw the erroe message " non-static method add(double,double) cannot be referenced from a static context".
    the program is:
    interfare pgm:
    import java.rmi.*;
    public interface intf extends Remote
    {
    double add(double d1,double d2) throws RemoteException;
    }
    implementation pgm:
    import java.rmi.*;
    import java.rmi.server.*;
    public class impl extends UnicastRemoteObject implements intf
    {
    public impl() throws RemoteException
    {}
    public double add(double d1,double d2)throws RemoteException
    {
    return d1+d2;
    }
    }
    server pgm:
    import java.rmi.*;
    import java.net.*;
    public class serv
    {
    public static void main(String args[])
    {
    try
    {
    impl implref=new impl();
    Naming.rebind("serv",implref);
    }
    catch(Exception e)
    {
    System.out.println("Exception:"+e);
    }
    }
    }
    client pgm:
    import java.rmi.*;
    public class clie
    {
    public static void main(String args[])
    {
    try
    {
    String surl="rmi://"+args[0]+"/serv";
    intf intfref=(intf)Naming.lookup(surl);

    System.out.println("the first number is:"+args[1]);
    double d1=Double.valueOf(args[1]).doubleValue();

    System.out.println("the second number is:"+args[2]);
    double d2=Double.valueOf(args[2]).doubleValue();

    System.out.println("the sum is:" + intf.add(d1,d2));
    }
    catch(Exception e)
    {
    System.out.println("Exception:"+e);
    }
    }
    }

    plz tell the correction as soon as possible.
    i am waiting for the result.............

  2. #2
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    A static method can only invoke instance methods through an instance of the class. Either make the instance method static, or instantiate the class and use the instance to access the method.

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

    Default

    This is one of the fine tutorial found on the web in the past, about static members. If you can just look at it.

    The Essence of OOP using Java: Static Members

    And also, you've post your question on wrong place. Please don't do it again. Choose the correct sub-forum before posting again. So in that way you can find much better solutions for your question. Good luck!

  4. #4
    cravi85 is offline Member
    Join Date
    Mar 2009
    Posts
    2
    Rep Power
    0

    Default

    thanks...........

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

    Default

    Welcome. If you have solve the problem please mark the thread solved. It really helpful to all other members as well, who's looking into your thread.

  6. #6
    Aseem is offline Senior Member
    Join Date
    Mar 2009
    Location
    USA
    Posts
    127
    Rep Power
    0

    Default

    hi

    i think the interface you are implementing is static.
    your method double is non- static and trying to get referenced from static one.
    so either you make ur method static or make the class non-static from where u r tryin to get referenced from.

Similar Threads

  1. Replies: 3
    Last Post: 03-20-2009, 01:35 AM
  2. Replies: 11
    Last Post: 11-19-2008, 04:13 AM
  3. Non-Static method in static context error
    By wizmang in forum New To Java
    Replies: 4
    Last Post: 04-24-2008, 09:51 AM
  4. Replies: 1
    Last Post: 08-07-2007, 06:05 AM
  5. Replies: 1
    Last Post: 08-01-2007, 10: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
  •