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
jteam12jteam12 

Basic Question: Assigning Value to Class Variable

Hi,

 

I have a very basic APEX question.  How do I assign values to class variables?  Take the example below.  The last two lines give a compile time error of "unexpected token: '=' ".  The code below is just a simple snippet that I've created to demonstrate the problem that I'm having.  The actual code that I am working with is a generated class from importing a WSDL.  In my code, I'm trying to create an instance of the message class so that I can pass it to the service invocation method.  But, as you can see, I'm getting stuck just trying to assign values to the class.

 

 

public class exampleC {
public class outerClass{
public innerClass IC;
}
public class innerClass{
public String str;
}

outerClass out = new outerClass(); //not sure if this is necessary since I have not created a constructor 

 out.IC = new innerClass();

 out.IC.str = 'A string';  //it seems like this statement should be sufficient without the one above it.  

 

}

 

Best Answer chosen by Admin (Salesforce Developers) 
Nick34536345Nick34536345

The syntax is fine, its just your last 3 lines of code are in no man's land. It ought to be inside a constructor or method.

e.g this compile  fine:

 

 

public class exampleC {
  public class outerClass{
    public innerClass IC;
  }
  public class innerClass{
    public String str;
  }
  public void test() {
    outerClass out = new outerClass(); 
    out.IC = new innerClass();
    out.IC.str = 'A string';  
  }
}