Instance Variable in Peoplecode - Application packages

It might be confusing for many of them, how is instance variables different from the Private and Public variables.

Myth :- specific object (instance of a class) would only have access to its own instance variables.

Instance variable is very different to Private or Public. Infact it is not right comparison of Instance with Private and Public.


Instance variables equal to Static variables in the other Object oriented programing language (like C++ and Java).

Instance - Defines the scope of the variable where as private and public key words define access levels/Visiblity of the variables. If variable is not defined as Instance then SCOPE of the variable is Local by defult (Local to the created object of the class).

Instance - scope of the variable is across the objects of the same class and scope of the Instance variable is at the class level.

The variables are created when the first object of the class is creted and will be present till last object of the same class is present in the memory.

If the Instance variable is declared as Private it is accessed the way it is accessed i,e only with in the class methods. if variable is declared as Public it is accessed the way you access other public variables. (i,e both within the class and outside of the class).

Simple comparison that can be done..(just for understanding purpose).

Component variables has scope within the component similarly instance variables has scope at the class level across the objects of the same class.

Big question arise when do we need these kind of variables.

1) if you want to count how many objects of the class was created you increment this variable in the constructor of the class.
2) If you have constant value that needs to shared across the objects of the class then you can use the instance variables.
3) Shared variable across the objects like component variable (across the different events of the component).

 

7 comments:

  1. AG said,

    one of my friend read this and started correlating component variable to an Instance variable.

    and object is identified by its class, again if multiple instances of the same class are present as different objects each instance variable is different than the other one.

    on September 16, 2010 at 11:20 AM  


  2. kiran said,

    I wrote the below code inside one Application Package whose name was ZC_TEST

    class zc_test
    method zc_test();
    method printcount();
    private
    instance number &objectcount;
    end-class;

    method zc_test
    &objectcount = &objectcount + 1;
    end-method;

    method printcount
    MessageBox(0, "", 0, 0, "The value of &objectcount = " | &objectcount);
    end-method;

    Then created one small Application Engine program to access the same class using the below code:
    import ZC_TEST:*;

    Local ZC_TEST:zc_test &obj1 = create ZC_TEST:zc_test();
    &obj1.printcount();
    Local zc_test:zc_test &obj2 = create zc_test:zc_test();
    &obj2.printcount();

    But the out of the program is not as desired.

    PeopleTools 8.50.09 - Application Engine
    Copyright (c) 1988-2010 PeopleSoft, Inc.
    All Rights Reserved


    The value of &objectcount = 1 (0,0)
    Message Set Number: 0
    Message Number: 0
    Message Reason: The value of &objectcount = 1 (0,0) (0,0)

    The value of &objectcount = 1 (0,0)
    Message Set Number: 0
    Message Number: 0
    Message Reason: The value of &objectcount = 1 (0,0) (0,0)
    Application Engine program Z_TEST ended normally

    After looking at the log file I did not feel that instance variables are same like static variables. Please suggest.

    Thanks,
    -Kiran P

    on October 1, 2010 at 7:55 AM  


  3. Anonymous said,

    AG,

    If each instance variable is different from another one, what is the difference from normal variable and instance variable ?

    Private instance variable and
    private variable - how are they different ?

    on October 5, 2010 at 1:43 AM  


  4. Anonymous said,

    Are you really sure peoplesoft Instance variables equal to Java static variables? I think Peoplesoft Instance variable is the opposite of Java static variables.

    on March 9, 2014 at 5:41 AM  


  5. kane81 said,

    Sorry but instance variable are != static variables. That is just plain wrong.

    See here more about private variables:


    http://jjmpsj.blogspot.com.au/2014/07/private-app-class-members.html

    on November 17, 2014 at 6:24 PM  


  6. kane81 said,

    if you wanted to do

    1) if you want to count how many objects of the class was created you increment this variable in the constructor of the class.

    you would need to declare the variable outside the class as global scope and increment the counter in the constructor of the class.

    on November 17, 2014 at 6:27 PM  


  7. Ganesh said,

    Hi All,

    Sorry for the confusion Instance variable is not the static variable, similar to Java application, hope it was same, but not.

    Private variable cannot manipulated by other objects.
    Private Instance Variable can be manipulated by other objects of the same class and not by objects on the different class.

    on July 29, 2015 at 4:53 PM