2

While Executing Apex Class only Top Level Class should Be Declared as Static in Test Class,Why this Error is Occuring ,How to Avoid this Error?

@isTest
public   class BusinessProfile1 {
public   void  BusinessProfile1(){
 public static String pgid;
 public static String plid;
 public static List<PG_SupportedCountry__c> psc1 = new List<PG_SupportedCountry__c>();
 public static List<PG_SupportedLanguage__c> psl1 = new List<PG_SupportedLanguage__c>();

How to avoid this Error:Error: Compile Error: Only top-level class variables can be declared static at line 3 ,Why this Error is Occuring?

0

2 Answers 2

2

These variables must be declared as static before your method:

public static String pgid;
public static String plid;
public static List<PG_SupportedCountry__c> psc1;
public static List<PG_SupportedLanguage__c> psl1;

You can then use instances of them or modify their values from within any method inside your class.

2
  • How to Declare Class As Static? If Ypu Declare Class as Static No Need to Describe Method and attributes inside the Class as Static ?it will automatically assign everything as Static ?
    – user30475
    Commented Apr 25, 2016 at 13:09
  • You can't declare the class as static, but you can declare the method as static instead of void. That being said, you don't need to change the BusinessProfile1() method to static in order to change the values of your static variables. You can also declare a sub-class that would use instance variables of the same name using the this keyword.
    – crmprogdev
    Commented Apr 25, 2016 at 13:17
0

APEX only allows static variables to be declared at the outer class, never in an inner class or method.

So ..

@isTest
public   class BusinessProfile1 {
  public static String pgid;
  public static String plid;
  public static List<PG_SupportedCountry__c> psc1 = new List<PG_SupportedCountry__c>();
  public static List<PG_SupportedLanguage__c> psl1 = new List<PG_SupportedLanguage__c>();    

  public   void  BusinessProfile1(){ ..
     // refer to pgId, etc here
  }
}

You must log in to answer this question.