1

Controller Class: TestController.cls

public class TestController {
    private integer num { get; set; }

    public TestController() {
        num = 100;
    }

    /* If I define the getter as below everything works fine
     public integer getNum() {
        return num;
    } */

}

VisualForce Page: TestPage.vfp

<apex:page controller="TestController">
    {!num}
</apex:page>

The above throws the following error.

Unknown property 'TestController.num'

  • Create Apex property 'TestController.num'
  • Create Apex method 'TestController.getNum'

I am not sure why I have to declare getters again just to use it in VisualForce page. Am I missing anything in {get; set;} syntax ?

2 Answers 2

2

Your variable is private in order to access your variable this should be public..

private integer num { get; set; }

Change to

public integer num { get; set; }

Access Modifiers

private

This is the default, and means that the method or variable is accessible only within the Apex class in which it is defined. If you do not specify an access modifier, the method or variable is private.

protected

This means that the method or variable is visible to any inner classes in the defining Apex class, and to the classes that extend the defining Apex class. You can only use this access modifier for instance methods and member variables. Note that it is strictly more permissive than the default (private) setting, just like Java.

public

This means the method or variable can be used by any Apex in this application or namespace.

global

This means the method or variable can be used by any Apex code that has access to the class, not just the Apex code in the same application. This access modifier should be used for any method that needs to be referenced outside of the application, either in the SOAP API or by other Apex code. If you declare a method or variable as global, you must also declare the class that contains it as global.

Apex Properties

5
  • Will it make the statements { get; set; } after the variable also private ? Feb 21, 2016 at 8:42
  • @karthikselva If your variable is { get; set; } with private access modifier. It will not be accessible in VF page.. If it is public then it will be accessible in VF page.
    – Ratan Paul
    Feb 21, 2016 at 8:44
  • Cool its working. Thanks I was under the impression getters are always declared public as in Java. Feb 21, 2016 at 8:48
  • @karthikselva cool.. You can check the docs for more info
    – Ratan Paul
    Feb 21, 2016 at 8:49
  • sure I am getting an message can accept in 1 minute. Thanks for the detailed edit Feb 21, 2016 at 8:53
2

Apex allows you to use the private, protected, public, and global access modifiers when defining methods and variables.

private This is the default, and means that the method or variable is accessible only within the Apex class in which it is defined. If you do not specify an access modifier, the method or variable is private.

protected This means that the method or variable is visible to any inner classes in the defining Apex class, and to the classes that extend the defining Apex class. You can only use this access modifier for instance methods and member variables. Note that it is strictly more permissive than the default (private) setting, just like Java.

public

This means the method or variable can be used by any Apex in this application or namespace.

So in your case if you want it to accessible in visualforce page. You need to define as public

public integer num { get; set; }

You can read more about access modifier and there behaviour from Apex Documentation

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.