Results 1 to 6 of 6
  1. #1
    Hamamshady is offline Member
    Join Date
    Mar 2013
    Posts
    3
    Rep Power
    0

    Post An issue of making a static reference to a non-static method

    I am quite new Java, and all programming language
    I also have a very bad internet connection, so forgive me if the answer is already here, I wasn't able to check all the other threads
    - I promise I checked as much as this internet could let me before I drove crazy -
    I was having this issue
    Java Code:
    import java.util.Scanner;
    
    
    public class Support {
     double W;
     double d1,d2;
     double r1,r2;
     
     
     public Support (double beamWeight, double supportr1, double support r2)
     { 
    	 W = beamWeight;
    	 r1 = supportr1;
    	 r2 = supportr2;
     }
    
     void answerReaction()
     {
    	 
    	 System.out.println("Reaction at first support : " + ((W*(5-d1))/(d2-d1)));
    	 System.out.println("Reaction at second support : " + (W-r1));
    	 
     }
     
     public static void main(String[] args){
    	 
    	 Scanner scan = new Scanner(System.in);
         System.out.println("Enter d1");
    	 double d1 = scan.nextInt();
    	 System.out.println("Enter d2");
    	 double d2 = scan.nextInt();
    	 System.out.println("Enter W");
    	 double W = scan.nextInt();
         Support.answerReaction();
    
     }
     
    }
    "Cannot make a static reference to the non-static method answerReaction() from the type Support"
    it want to change the
    " void answerReaction() " to " static void answerReaction()"
    if that happens then I can't get the new r1 and r2 results :D
    how to over come this ?
    and is this logic even possible ? :D
    this is my first program right after the hello world one, a scum-bag tutor :D
    Last edited by Hamamshady; 03-31-2013 at 01:08 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: An issue of making a static reference to a non-static method

    You're calling the answerReaction method on the Support class as if it were a static method, but it's not. It's an instance (non-static) method and needs to be called off of a Support *object*. So in your main method, create a Support object with the data that you've gathered from the user, and then call the answerReaction() method on it.

  3. #3
    Hamamshady is offline Member
    Join Date
    Mar 2013
    Posts
    3
    Rep Power
    0

    Default Re: An issue of making a static reference to a non-static method

    Java Code:
    import java.util.Scanner;
    
    
    public class Support {
      double W;
      double d1,d2;
      double r1,r2;
      
      
    public Support (double supportd1, double supportd2, double beamWeight)
    {
    d1 = supportd1;
    d2 = supportd2;
    W = beamWeight;
    r1 = ((W*(5-d1))/(d2-d1));
    r2 = (W-r1);
    }
    
    
    public void answerReaction()
    {
    System.out.println("Reaction at first support : " + r1);
    System.out.println("Reaction at second support : " + r2);
    }
    
    
    public static void main(String[] args){
     Support support1;
     Scanner scan = new Scanner(System.in);
     System.out.println("Enter d1");
     double d1 = scan.nextInt();
     System.out.println("Enter d2");
     double d2 = scan.nextInt();
     System.out.println("Enter W");
     double W = scan.nextInt();
     support1.Support(d1,d2,W);
     support1.answerReaction();
    }
    
    }
    Now it says "The method Support(double, double, double) is undefined for the type Support"
    @ the " support1.Support(d1,d2,W);" line :D

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: An issue of making a static reference to a non-static method

    Have you studied how to call constructors using the new keyword yet? If not, have a look in your text or tutorials on this, because this is what you need to do -- call the Support constructor, for this is no Support method, and so you can't try to call the constructor like a method.

  5. #5
    Hamamshady is offline Member
    Join Date
    Mar 2013
    Posts
    3
    Rep Power
    0

    Default Re: An issue of making a static reference to a non-static method

    Nope, I didn't study that :D
    And we neither have a text nor tutorials in this subject :D - issues with the tutor, only slides, and we didn't come across such thing so far -
    I will just try to look online for some tutorials, if you have any suggestions of tutorials about the "new" keyword
    I would like to ask you to share them :D
    Thanks in advance :D

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. static reference to non static method
    By TSTONESIX in forum New To Java
    Replies: 7
    Last Post: 03-13-2013, 03:07 PM
  2. Cannot make a static reference to the non-static method
    By Reskaillev in forum New To Java
    Replies: 4
    Last Post: 08-02-2011, 12:42 AM
  3. Garbage Collection - Self reference, static and non-static.
    By garyiskidding in forum Advanced Java
    Replies: 10
    Last Post: 03-17-2011, 11:22 AM
  4. Replies: 10
    Last Post: 11-14-2010, 07:06 AM
  5. Replies: 7
    Last Post: 09-24-2010, 05:09 AM

Posting Permissions

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