CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   Java and JSP (http://www.codingforums.com/forumdisplay.php?f=54)
-   -   non-static method addCar() cannot be referenced from a static context (http://www.codingforums.com/showthread.php?t=288348)

trancecommunity 02-26-2013 08:20 PM

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

Fou-Lu 02-26-2013 09:59 PM

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.

trancecommunity 02-26-2013 10:30 PM

Ah nice. Just had to create the object.

Thanks for the advice.


All times are GMT +1. The time now is 06:37 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.