Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Salesforce
  3. Question
Salesforce

How to resolve error non static method cannot be referenced from a static context?

Asked by Bruce Benedict Aug 17, 2021 47.9K views 3 answers
Share

About this question

Here is the Test class:

@isTest private with sharing class Genereratortest { @TestSetup static void createPayloadtest() { List<_Site__c> studySites = new List<_Study_Site__c>{ TestDataFactory.createStudySite('test','Data'), }; Test.startTest(); SSUDataJSONGenerator.createPayload(studySites,'INSERT'); Test.stopTest(); }
} }

When I run this test class I am getting below error

The nonstatic method cannot be referenced from a static context: String SSUDataJSONGenerator.createPayload(List, String)

Not sure what’s going wrong with the test class, please suggest a possible solution.


Your answer

3 Answers

Ranjana Admin JanBask Expert Latest answer

Answered on Feb 3, 2025

The "non-static method cannot be referenced from a static context" error in Java occurs when trying to call a non-static method from a static method (like main()) without creating an instance of the class.


Causes of the Error

Calling a Non-Static Method from main() Directly

class Example {
    void display() {
        System.out.println("Hello, World!");
    }
    public static void main(String[] args) {
        display(); // ❌ Error: Cannot call non-static method from static context
    }
}

display() is non-static, but main() is static, causing an error.

Solutions

✔ Solution 1: Create an Object of the Class

Create an instance before calling the method.

class Example {
    void display() {
        System.out.println("Hello, World!");
    }
    public static void main(String[] args) {
        Example obj = new Example();
        obj.display(); // ✅ Works fine
    }
}

✔ Solution 2: Make the Method Static

If the method does not depend on instance variables, declare it as static.

class Example {
    static void display() {
        System.out.println("Hello, World!");
    }
    public static void main(String[] args) {
        display(); // ✅ Works fine
    }
}

✔ Solution 3: Pass an Object to a Static Method

If the method is non-static, pass an instance of the class to call it.

class Example {
    void display() {
        System.out.println("Hello, World!");
    }
    static void callMethod(Example obj) {
        obj.display(); // ✅ Works fine
    }
    public static void main(String[] args) {
        Example obj = new Example();
        callMethod(obj);
    }
}

Best Practices

  • Use static methods for utility functions that don’t rely on instance variables.
  • Use instance methods when working with object-specific data.

Would you like more details?

Was this helpful?

Ranjana Admin JanBask Expert

Answered on Apr 22, 2024

To resolve the error "Non-Static Method Cannot Be Referenced From a Static Context," you can do one of the following:


Make the Method Static: If the method does not rely on instance-specific data, you can declare it as static. This allows it to be called from a static context.

public static void myMethod() {
    // Method implementation
}

Create an Instance of the Class: If the method is non-static and belongs to a class, you need to create an instance of the class to access the method.

Copy code
MyClass obj = new MyClass();
obj.myMethod(); // Call the non-static method using the object

Convert the Calling Method to Non-Static: If the method calling the non-static method is static, consider making it non-static or creating an instance of the class containing the non-static method.

By applying one of these approaches, you can resolve the error and ensure that the non-static method can be referenced correctly.


Was this helpful?

More Salesforce discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Salesforce Blogs

Guides, tips and career advice on Salesforce from JanBask experts.