---
schema_version: "1"
id: 95f252f1a7d219b184db9294b8303cf2c1e1174219aeadcbae8a0436ebae8a7f
title: Access Controllers From Visualforce Pages in Different Packages
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/platform/salesforce-pages-developers-guide/guides/pages/packaging/pages-packaging-2gp.md
canonical_url: https://developer.salesforce.com/docs/platform/salesforce-pages-developers-guide/guide/pages-packaging-2gp.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: 5384f039b874f1f2e146c9ad83d44b2a9c68fb523d8b7fc839980fe1797b9e4f
products:
  - Visualforce Developer Guide
  - Platform Developer Content
---

# Access Controllers From Visualforce Pages in Different Packages

To access an Apex controller from a Visualforce page that is in a different package, use the `@namespaceAccessible` Apex annotation in your custom controller class. In first-generation packaging, you can develop only one managed package with a given namespace. In second-generation packaging, you can develop more than one managed (or unlocked) package with the same namespace. By default, Visualforce pages installed in a package can't call a public Apex method from an Apex class in another package. This is true even if both packages are in the same namespace.

Here’s an example of how to include the `@namespaceAccessible` annotation in your Apex code.

```apex
@namespaceAccessible
public virtual class NsController {
    private String message;
    @namespaceAccessible
    public NsController() {
        this.message = 'default'; // init to non-blank value
    }
    @namespaceAccessible
    public virtual String getMessage() {
       return this.message;
    }
    @namespaceAccessible
    public virtual void setMessage(String msg) {
        this.message = msg;
    }
}

```

First, add the annotation above the controller, which makes it visible from across the namespace in different packages. You must annotate the controller so that any methods you add to the annotation are also visible. Then, for every method that you want visible across the namespace, add the `@namespaceAccessible` annotation before the method. Use the annotation only for methods that you want visible outside of your package, but within the same namespace.

