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
Apex BeginnerApex Beginner 

Apex class in anonymous window

Hello,

I am beginner in Salesforce and still in initial steps in understanding the concepts..i tried the below class and the output didnt come as expected 

public class mytest2 
{
    Public integer marks;
    Public string result;
        {
        if(marks >= 35 && marks <= 50)
        result = 'passed';
        else if (marks >= 51 && marks <= 70)
        result = 'B';
        else if (marks >= 71)
        result ='A';
        else 
        result = 'failed';
                {
                  system.debug('Your results:' + result);
                }
        }    
}

and executing it in anonymous window 

mytest2 c1 = new mytest2();
c1.marks = 55;
system.debug(c1);

Output was Your results:failed

which seems to be wrong ..not sure what went wrong here
Naga  AlapatiNaga Alapati
Hi,

So you place the logic in Instance initialization code, which is executed each time an object is instantiated from that class. This code runs before the constructor and executed only once
 
mytest2 c1 = new mytest2(); // so here it executes your logic and the value of marks variable is null here, so it populates the result variable with "failed" value.
c1.marks = 55;  // Here you are defining the marks value, but your instance initialization code is not executed here.
system.debug(c1);

For example, if you declare marks variable with some value like below 
public class mytest2 
{
    Public integer marks = 55;
    Public string result;
 
    {
        if(marks >= 35 && marks <= 50) result = 'passed';
        else if (marks >= 51 && marks <= 70) result = 'B';
        else if (marks >= 71) result ='A';
        else result = 'failed';
    }     
}
and if you execute the following code in anonymous window

mytest2 c1 = new mytest2();
mytest2 c1 = new mytest2();
system.debug(c1);
you can see the output as 
19:48:52:009 USER_DEBUG [2]|DEBUG|mytest2:[marks=55, result=B]

One more thing to remember is that the instance instalization code runs before the constructor. For example if you create a constructor for your test class to take the marks value as an argument, like this
 
public class mytest2 
{
    Public integer marks;
    Public string result;
    
    public mytest2(Integer m) {
        marks = m;
    }
    
    {
        if(marks >= 35 && marks <= 50) result = 'passed';
        else if (marks >= 51 && marks <= 70) result = 'B';
        else if (marks >= 71) result ='A';
        else result = 'failed';
    }     
}
Suppose if you execute the following code in anonymous window, you will get the failed value only.
mytest2 c1 = new mytest2(55);
system.debug(c1);
 
I think you can use apex class methods to take marks as an argument and return the result as output.

Regards,
Naga
Christan G 4Christan G 4
For something like this, you'll have to use setters and getters methods to set a value to the marks integer property and then use a get method to define what you actually want to do with that value and what you want returned.

I have provided an example code below for guidance. If you have any questions, please feel free to comment further.

Apex Class Code:
public class mytest2 {

    //Object Properties
    public static integer marks;
    public static string result;
    
    
    //Set Method for Integer marks
    public void setMarks(integer num) {
        
        marks = num;
    }
    
    //Get Method for how marks should be evaluated and what should be returned.
    public String getResults() {
        
        if(marks >= 35 && marks <= 50) { 
        result = 'passed';}
        
        else if (marks >= 51 && marks <= 70) { 
        result = 'B'; }
        
        else if (marks >= 71) { 
        result ='A'; }
        
        else { 
        result = 'failed';}
        
        return 'Your results: ' + result;
        
    }
    
}

Anonymous Block Code: 
mytest2 c1 = new mytest2();
c1.setMarks(55);
system.debug(c1.getResults());
Akshay Agarwal 36Akshay Agarwal 36
Hi //Try this code. Hope this works. If worked then mark it as the best answer

public class mytest2 
{
    public static string result (Integer marks) {
    String result = '';
        if(marks >= 35 && marks <= 50)
           result = 'passed';
        else if (marks >= 51 && marks <= 70)
           result = 'B';
        else if (marks >= 71)
           result ='A';
        else 
           result = 'failed';
               
       system.debug('Your results:' + result);
                
        }    
}