Results 1 to 3 of 3
  1. #1
    Clown is offline Member
    Join Date
    Mar 2010
    Posts
    9
    Rep Power
    0

    Default non static method cannot be referenced......

    Hi guys, I just wrote a simple class Stackkalkulator.

    Java Code:
    import java.util.*;
    
    public class Stackcalculator extends java.util.Stack
    
    {
        public LinkedList<Double> stack = new LinkedList<Double>();
        
       
        public Stackcalculator() {
        }
       
        public void push(Double d)
        {
            stack.push(d);
        }
       
        public Double pop()
        {
            return stack.pop();
        }
       
        public void add()
        {
            stack.push(stack.pop() + stack.pop());
        }
       
        public void sub()
        {
            double result = stack.pop();
            result = stack.pop() - result;
            stack.push(result);
        }
       
        public  void mul()  
        {
            stack.push(stack.pop() * stack.pop());
        }
       
        public  void div()
        {
            double result = stack.pop();
            result = stack.pop() / result;
            stack.push(result);
        }
        public  void show(){
            
          int a=stack.size();
          double [] c=new double [a];
          for (int i=0;i<a; i++){
              c[i]=stack.pop();
              System.out.println(c[i]);
            }
                
            
        }
        
    }
    Now I am trying to create a new class test


    Java Code:
    import java.util.*;
    
    public class Test extends junit.framework.TestCase
    {
      
        
        public boolean test(){
        Stackcalculator.stack.push(3.0);
        Stackcalculatorstack.push(5.1);
        Stackcalculator.add();
        }
      
    }
    but I am getting the following error
    non static method cannot be referenced from a static context.

    Would appreciate any help in solving this.
    Kind Regards,
    Deiv

    Moderator Edit: Code tags added
    Last edited by Fubarable; 03-21-2010 at 06:57 PM. Reason: Moderator Edit: Code tags added

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

    Default

    First of all, the stack field should be private not public. Next of all, why are you having this class both extend Stack and use an internal LinkedList. I think that you'll need to decide on extension through inheritance or composition and stick with one or the other. Finally, make an object of your class in the Test class and call methods on this object, not on the class itself.

    i.e.,
    Java Code:
          Stackcalculator sCalc = new Stackcalculator();
          sCalc.push(3.0);
          sCalc.push(5.1);
          sCalc.add();
    Last edited by Fubarable; 03-21-2010 at 07:00 PM.

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,367
    Blog Entries
    7
    Rep Power
    20

    Default

    Sorry, I think that code is a mess OO wise speaking: your Stackcalculator is a stack because it inherits from the Stack class and it also has a stack; it's overriding push( ... ), pop( ... ) methods delegate to its member stack. That's highly confusing at best. I didn't look any further; please refactor your code to something more understandable.

    kind regards,

    Jos

    edit: and who's the slowest old sod again?

Similar Threads

  1. non-static method cannot be referenced from a static context.
    By blackstormattack in forum New To Java
    Replies: 5
    Last Post: 05-07-2009, 04:05 AM
  2. Replies: 3
    Last Post: 04-11-2009, 09:14 PM
  3. non-static method cannot be referenced
    By Taz_84 in forum New To Java
    Replies: 19
    Last Post: 12-30-2008, 11:35 AM
  4. Replies: 11
    Last Post: 11-19-2008, 03:13 AM
  5. Replies: 1
    Last Post: 08-07-2007, 05:05 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
  •