You need to sign in to do that
Don't have an account?
sukumar
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 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.
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
How are you executing the class? I created your class...
And executed anonymous the following statments, with no problem.
Can you provide a little more information on what you are running when you see that error?
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?.
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
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.
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!
Static variables can only be saved on the top-level class. For example:
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.
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.
You can replicate my error by not following these two points.
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..
You can call a static method only by class name and another method can call by its instance.
Like this:
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi