‎2007 Feb 08 3:14 AM
hi, need to seek clarification on the follow
1) method class_constructor - will execute this very first and 1 time only if this method is defined. correct?
2) if 1 is correct, why need this method?
3) method constructor - immediately when create object, this method will be executed. correct?
4) when 3 is executed, all the private section variable will be initialised except class-data variable. correct?
5) for point 4, what about public section variable? will also be initialised?
6) the value stored during create object for private section variable will exist in that object. when next create object, the private section variable will be initialised so that another value will be stored for subsequent object. correct?
thanks
‎2007 Feb 08 3:21 AM
‎2007 Feb 08 3:39 AM
HI,
1) YES.
2) This method is generally used to fetch the values for the STATIC DATA OF THE CLASS (CLASS-DATA). If you have some static attributes of class that are used as constant values by the users of this class then these attributes should contain a value before anybody wants to use them. SO as and when you try to use this class, CLASS-CONSTRUCTOR will be executed and inside that you populate these attributes. Once CLASS_CONSTRUCOTR is executed its work is done there is no use of calling it again so it is decided that it will be called only once.
3) YES
4) NO, BY the time CONSTRUCTOR is executed CLASS_CONSTRUCTOR would have already been executed automatically by the runtime so the CLASS-DATA is already initialized. CONSTRUCTOR is used to assign data to INSTANCE-DATA(Not only Private but also public and protected).
5) YES (All the instance attributes irrespective of PUBLIC, PRIVATE or PROTECTED will be intialized based on the values you pass to constructor or based on their TYPE.
6) NO, All that is not declared with CLASS-DATA is instance level means every object has its own private, public and protected data. ONLY CLASS-DATA is shared across all the instances.
Regards,
Sesh
Message was edited by:
Seshatalpasai Madala
‎2007 Feb 08 3:58 AM
hi,
thanks for the reply.
1) may i know if class-data variable value can get changed right? meaning during instancing of the same class, the class-data variable will not initialised but the value will change depending on the value assign to class-data variable during instancing.
for example, during first instancing, the value in class-data variable is 40. then when the next instancing starts, the value in class-data variable is 40 also. if in this instance, there is a value assignment maybe to 50, then class-data variable will become 50.
2) whereas when there is a new instancing starts, as what gurus here have mentioned, private / public or protected variable will be initialised. During the instancing, value that assigned to private / public or protected variable will retain in this instance.
thanks again
‎2007 Feb 08 4:09 AM
Hi,
1) The CLASS-DATA can only access and changed in CLASS-METHODS. Now coming to your example say value 40 is assigned in the CLASS-CONSTRUCTOR.
Then instance 1 has te value as 40. Then instance2 cannot change the value using normal method. It can change using CLASS METHOD. Now lets assume that instance 2 has changed the value to 50 using a CLASS METHOD from that time instance 1 will also see the changed value. Now if you dont want this value to be changed by any instance just make it FINAL (READ-ONLY).
2) During instantiating all the attributes does not contain any value only after the CONSTRUCTOR these attributes will get initialized. EACH OBJECT INSTANCE HAS ITS OWN DATA UNLESS IT IS CLASS_DATA. Yes the value that you pass to the constructor and that you assign to the attributes in the construcot will be the initial value for each attribute. If you dont assign any value for any attrobite in the constructor then it will get its initial value from its type. SAY your attribute is of TYPE CHAR. then that aatribute will have SPACE as the initial value unless you pass some other value using the constructor.
Regards,
Sesh
Message was edited by:
Seshatalpasai Madala
Message was edited by:
Seshatalpasai Madala
‎2007 Feb 08 4:23 AM
hi sesha and all,
thanks.
1) your statement "The CLASS-DATA can only access and changed in CLASS-METHODS"
in my program, i have a class-data variable, say score. i have this statement in constructor method as score = score + 1. so it also can get changed by other method, i think only constructor method during instancing. correct?
2) if the class has no constructor method, then all variable will be intial value. but if the class has constructor method, then each instance has its own value retain in the private/public/protected variable. when next instance, variable initialised and contructor method assigning new value to the variable. correct?
thanks
‎2007 Feb 08 4:31 AM
HI,
1) As I said if the data is defined using CLASS-DATA or in SE24 the attribute is marked as STATIC then only methods that are defined as CLASS-METHODS or that are selected as STATIC in SE24 can change this DATA. You should not be able to access tis data in CONSTRUCTOR but you can in CLASS_CONSTRUCOTR.
2) Yes if the class has no CONSTRUCTOR then all its INSTANCE attributes will get their initial value. IF the class has CONSTRUCOTR and there are IMPORTING parameters for this constructor to accept values for each of the instance attribute and inside the construcotr if you assign the importing parameter to your instance attribute then it will get the value you pass otherwise it will have initial value of its TYPE. CONSTRUCTOR is also a INSTANCE METHOD each instance has its own constructor method. SO there is no connection between one instance to another instance.
Regards,
Sesh
‎2007 Feb 08 4:42 AM