Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Feb 2013
    Posts
    45
    Thanks
    8
    Thanked 0 Times in 0 Posts

    non-static method addCar() cannot be referenced from a static context

    Trying to call this function. Point being it adds an object called carNew into an array and increases total by 1. Then returns a "true" that can print out a success message.


    Code:
    boolean ok = CarShowRoom.add(carNew);
    Code:
    public boolean add(Car carIn)
        {
    
            if ( !isFull(total) ){
    
                cars[total] = carIn;
    
                total++;
            }
            else{
                return false;
            }
    
            return true;
    
        }
    Not sure what other info you need. Just ask.

    Thanks

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    public boolean add(Car carIn);
    The signature here indicates that this is an object context method, but the call is that on CarShowRoom.add which is static. You need to construct an instance of CarShowRoom in order to .add to it, or flag the method as static. If the cars array is static and the total is static, than the method can be flagged as static. Otherwise, you need to instantiate CarShowRoom in order to invoke the .add.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    trancecommunity (02-26-2013)

  • #3
    New Coder
    Join Date
    Feb 2013
    Posts
    45
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Ah nice. Just had to create the object.

    Thanks for the advice.


  •  

    Posting Permissions

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