function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sukumarsukumar 

Problem with top level class

I have created HelloWorld example class in developer console which is in apex work book


public class HelloWorld{
    public static void sayYou(){
        System.debug('you');
    }
     
    public void sayMe(){
    System.debug('Me');
    }
}


I think I have created top - level class and declared static method and instance method in that class.

But when I execute this class I am getting error saying that 'Only top-level class methods can be declared static'

Please help me if there is any wrong in my example. 
 
I am new to apex code.I think it is silly doubt but please help me.
souvik9086souvik9086

Hi Sukumar,

 

Try to execute it in the controller section(Apex Classes) and it will work fine.

 

If this post solves your problem , please mark it as solution.

 

Thanks

Thomas DvornikThomas Dvornik

How are you executing the class? I created your class...

 

public class HelloWorld{
    public static void sayYou(){
        System.debug('you');
    }
     
    public void sayMe(){
    System.debug('Me');
    }
}

 And executed anonymous the following statments, with no problem.

HelloWorld.sayYou();
new HelloWorld().sayMe();

 Can you provide a little more information on what  you are running when you see that error?

 

sukumarsukumar
Thanks for your answer.

I am running that whole code in the textbox next to the execute button on the top of the developer console.

It worked for me when I run using HelloWorld.sayYou() for static method and for instance method I created instance for class and called sayMe() instance method using instance variable.

Please tell me where can I run whole apex class.

I am new to this ..so I am asking small doubts.

Thanks & Regards,

Sukumar Reddy | Software Programmer
Sukumar.mandem@pbsystems.com
C: +91-9493757230
PB Systems - Results. Delivered?.
souvik9086souvik9086

Hi,

 

In 

Your name -> SetUp -> App Setup -> Develop -> Apex Classes

 Write the above class and create a Visualforce Page keeping as controller for this class

Your name -> SetUp -> App Setup -> Develop -> Pages

Then you can run the page either directly through url or through Custom Tabs.

 

If this solves your problem, kindly mark it as solution.

 

Thanks

 

 

 

Thomas DvornikThomas Dvornik

You can not execute anonymous classes. You must save the class to your org, as souvik suggessted or, via a tool such as the developer console or the Force.com IDE.

 

Once the class is saved, you can instantiate it or execute parts of it in the execute anonymous box, a VF page, a trigger, a rest request, etc.

ryzrryzr

Working through the Apex workbook.  Lesson 4:  Static Variables, Constants, and Methods

Modified the Fridge class by adding the following static variable:

 

public static Integer stockThreshold = 5;

 

Getting error:  Only top-level class variables can be declared static.

So how do I save from developer console?

 

Apex workbook does not mention saving the class.

 

Thx!

Thomas DvornikThomas Dvornik

Static variables can only be saved on the top-level class. For example:

 

public class Fridge {
    public static Integer stockThreshold = 5;
}

 I'm assuming you are trying to put it inside of an inner class or function, which Apex doesn't allow. I would need to see the code you are trying to save to know for sure.

ryzrryzr

Thank you for your reply.  I believe I got this figured out...but it could have been more clear in the Apex Workbook.  Pls see workbook for the Fridge class code declaring a static class variable.

 

  1. In the Developer Console always save and run your class from the Repository.
  2. To call it's class members methods and properties always use the immediate window.

You can replicate my error by not following these two points.

KEYUR SHAH 94KEYUR SHAH 94
if you trying to test class in console application where static variable and methods are there , You will get error with top level class...
same thing first you create class with same defination in paex class with user interface and then debug static variable in console ..it will execute without error..
here Top level class is Classes created with User Interface..
Ajay K DubediAjay K Dubedi
Hi Sukumar,

You can call a static method only by class name and another method can call by its instance.
Like this:

User-added image

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
KapavariVenkatramanaKapavariVenkatramana
Yes this is Correct Ajay