+ Start a Discussion
troutguytroutguy 

How to Reference Other Classes in Apex, like a typical Java import statement ?

Excuse me, as I'm seasoned Java programmer but new to Apex.

 

In Java you would have an import statement, like "import mypackage.thiscompany.com.*;"  The import statement is at top of class, right below package declaration.

 

How do you do an "import" in Apex?

 

What I want to do is reference another class for utilities.    Specifically there are some SFDC code share classes for JSON that I want to use from ANOTHER Apex class.  How to I point to those JSON classes in my own Apex class?

 

http://developer.force.com/codeshare/apex/ProjectPage?id=a0630000002ahp4AAA

 

Another example might be for JavaMail or JDOM; what if a person wanted to use those 3rd party packages via an import?

 

Best Answer chosen by Admin (Salesforce Developers) 
forecast_is_cloudyforecast_is_cloudy

Troutguy,

Apex does not have a concept of package names and imports as Java does. As long as you declare your class 'public' or 'global', you can reuse it in another class. In your case, you'll need to create the JSON parser class (click on 'View Source' from the Code Share link) in your instance of Salesforce (aka 'Organization'). You can do so by simply via  copy-paste. Another (an better) way of reusing other code/'libraries' is to have that code be available as an Package (Managed or Unmanaged). You can find more details on how to use Apex with pacakges here - http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_manpkgs_dev.htm.

In this case, since the code is not available as a package, your only option is to manually recreate the code in your Org. Hope this helps clarify.

 

p.s. Caveat emptor on the JSON library that you're trying to reuse. You'll most likely going to run into governor issues trying to use that library to parse JSON data (depends on the size of the data you  try to parse). I would wait for a native JSON parser in Apex (which is on the roadmap and should hopefully be released in the next couple of releases) if at all possible.

All Answers

forecast_is_cloudyforecast_is_cloudy

Troutguy,

Apex does not have a concept of package names and imports as Java does. As long as you declare your class 'public' or 'global', you can reuse it in another class. In your case, you'll need to create the JSON parser class (click on 'View Source' from the Code Share link) in your instance of Salesforce (aka 'Organization'). You can do so by simply via  copy-paste. Another (an better) way of reusing other code/'libraries' is to have that code be available as an Package (Managed or Unmanaged). You can find more details on how to use Apex with pacakges here - http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_manpkgs_dev.htm.

In this case, since the code is not available as a package, your only option is to manually recreate the code in your Org. Hope this helps clarify.

 

p.s. Caveat emptor on the JSON library that you're trying to reuse. You'll most likely going to run into governor issues trying to use that library to parse JSON data (depends on the size of the data you  try to parse). I would wait for a native JSON parser in Apex (which is on the roadmap and should hopefully be released in the next couple of releases) if at all possible.

This was selected as the best answer
Bhawani SharmaBhawani Sharma

Hi,

 

There is no directory structure in Salesforce. Whatever class you define all will be created under the same directory.

Once  you define a namespace for you organization it works like creating single directory and all the code will be under this.

 

So you can create an instance of the desired class directly.

like

MyClass classObj = new MyClass();

C_GibsonC_Gibson

I am running into an issue with this when using interfaces. When I create an interface in an entirely separate class I have to list the class name along with the interface name. for example:

public MyClass implements IMyInterfaceClass.IMyInterface {

 

}

 

When I want to make an object of this interface type I have to do the same thing of:

IMyInterfaceClass.IMyInterface object = new ObjectThatImplementsInterface();

This method works but I am just wondering why this is the case since the other posts seem to refer to their being no "Packages" like java. Is it treated as some sort of static variable?? 

Thanks