Java error: non-static method cannot be referenced from a static context


Recently, while writing a java program, I got the classic Java error:
non-static method cannot be referenced from a static context.

No doubt, other young java practitioners have had this error.

Here, I’ve laid out a simple program, with explanations, so that you can avoid the problem. Voila:

public class StaticIssues 
{  
    // At top of the class, only declare the global object 	
    // to be seen in the entire class
    objCount theCount = new objCount();

    // do not run any code here, such as:
    // System.out.println ( "top of class" );
    // or 
    // theCount.returnCount();

    // constructor of the class StaticIssues
    public StaticIssues () 
    {   // In Constructor, initialize the objects
        // including the other objects, such as 
        // instances of sub Object objCount

        theCount.initCount ();

        System.out.println ("StaticIssues () Constructor"  );
        
        // note that this code will run without this constructor
    }  // end constructor


    // code for sub-object objCount 
    // no brackets at the end of this declaration. ie. objCount ()
    public class objCount 
    {   
        // declare integer within class
        // not globally

        private int theCount = 0;

        public void initCount ()
            {	theCount = 0;
            }

        public void incrementCount ()
            {	theCount ++;
            }

        public int returnCount()
        {   return theCount;
        }  // end returnCount()

    }  // end class objCount 

    
    // other methods can use the object
    public void someMethod ()
    { 
        // In other methods, use the instance of the object
        // and call the methods of the object

        theCount.incrementCount ();
        System.out.print ( "In someMethod ()." );
        System.out.println ( "  theCount.returnCount() is now: " +
                theCount.returnCount()  );

        // However, do not reference the object itself. ie.
        // objCount.incrementCount ()  ;
        // otherwise, you get the famous:
        // line number of the error, followed by

        // error: non-static method incrementCount() cannot be 
        // referenced from a static context
        // objCount.incrementCount ()  ; 
        // 1 error

    }  // end someMethod ()


    public static void main(String []args) 
    {   
        // within main, create object
        StaticIssues objInstance = new StaticIssues ();

        // reference the object.
        System.out.println ( "objInstance.theCount.returnCount () is: " 
                + objInstance.theCount.returnCount () );

        objInstance.someMethod();

        // Note that from the main class 
        // you have to prefix the references by the 
        // object declared in the main class, 
        // here, objInstance
        System.out.println ( "objInstance.theCount.returnCount () is: " 
                + objInstance.theCount.returnCount () );

    }  // end main

    
}  // end class StaticIssues



Output:

StaticIssues () Constructor
objInstance.theCount.returnCount () is: 0
In someMethod ().  theCount.returnCount() is now: 1
objInstance.theCount.returnCount () is: 1

I’m sure that these guidelines will not solve EVERY occurrence of this error. And that there can be improvements to the program. But as I’ve written before, there is nothing a like good design and coding correctly in the first place.

Feel free to use this as a start.

Leave a comment