How to resolve the error “ non-static method cannot be referenced from a static context”?

163    Asked by DanPeters in Salesforce , Asked on Jul 31, 2023

The test class is given below:

@isTest private with sharing class Generatortest { @TestSetup static void create payload Test 

}}

On running the test , the below-mentioned error message is displayed

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

What is the solution?

Answered by Elizabeth Clarke

 The error message is seen by the freshers while compiling Java programs. They deploy a non static member variable in the main() method. The main method in Java refers to a static method and is called by default. Hence we don't need to make the object for calling it. An easy way to fix the error ``non-static variable cannot be referenced from a static context error” is by addressing the non-static variable by the object name. You must create an object of the class to denote the non static variable from the static context.


You need to make an instance of the class as follows:

Test.startTest();
SSUDataJSONGenerator generator = new SSUDataJSONGenerator();
generator.createPayLoad(studySites,’INSERT’);
Test.stopTest();
You can also alter the method to static
public static String createPayload(List sobjrecords, String operation) {
You also need to use @isTest to denote a unit test method. @testSetup is only for creating test data (if necessary).
@isTest
 class SSUDataJSONGeneratorTest {
 @isTest
 static void createPayloadtest() {
           List studySites = new List{
            TestDataFactory.createStudySite('test','Data'),
        };
        Test.startTest();
        SSUDataJSONGenerator.createPayload(studySites,'INSERT');
        Test.stopTest();
    }
}

The java compiler gives a compile time error message. The problem can be fixed by addressing the variables with the object names. On creating a fresh instance, a new copy of the non-static variables and methods are generated. Let’s have a look at an example.

// Java program to access a
// non static variable from
// a static block
public class GFG {
    int count = 0;
    // Driver code
    public static void main(String args[])
    {
        // Accessing static variable
        // by creating an instance
        // of the class
        GFG test = new GFG();
        test.count++;
        System.out.println(test.count);
    }
}

The Salesforce Certification Training offered at JanBask Training provides experience like offline classes to help candidates master the Salesforce Development and Administration in a better way to face the tough job market scenario. The course guarantees job success and lets you qualify the certification exam through intensive, and expert-led virtual sessions and hands-on practical assignments. The course will teach you to create and configure the salesforce account to help you gather, extract, and examine the data relevant to the customer base. You can also avail the Force.com platform to create and use the advanced cloud apps with utmost confidence.



Your Answer

Interviews

Parent Categories