05-23-2008 5:52 AM
Hi Experts
plz tell me what is difference in declaration b/t data anad class-data as both are defined for class only .
1 .data : inum type i .
class-data : cnum type i .
2 methods : m1 .
class-methods : m2.
interface i1 .
constants : c_name(4) type c value 'ABAP'.
data : inum type i .
class-data : cnum type i .
methods : m1 .
class-methods : m2.
endinterface.
thanks .
05-23-2008 3:54 PM
HI Ranjna,
You can see an example on following link [ABAP Objects - Creating your First Local Class - Using Static Components|https://wiki.sdn.sap.com/wiki/x/ZtM].
For more examples see [ ABAP Objects Examples Beginner|https://wiki.sdn.sap.com/wiki/x/pto].
Greetings,
Marcelo Ramos
05-23-2008 5:58 AM
05-23-2008 6:00 AM
Hi,
Instance attributes
Instance attributes are attributes that exist once per object, that is, once per
runtime instance of the class. They are defined with the syntax element
DATA.
Static attributes
Static attributes exist once for each class and are visible for all runtime
instances in that class. They are defined with the syntax element
CLASS-DATA.
Static attributes usually contain information that applies to all instances,
such as:
1. Types and constants
2. Central application data buffers
3. Administrative information, such as the instance counter
Technical literature often refers to static attributes as class attributes (compare to
the CLASS-DATA syntax element). In ABAP Objects, as in C++ and Java, the
official term is static attribute.
Regards,
Jagadish
05-23-2008 7:37 AM
Hi,
First of all, what is class? A class is a set of objects that have the same structure and same behavior. It is a blueprint based on which all the objects in that class are created.
Components of the class are : Attributes, methods, events, constants, types, and implemented interfaces. Only methods are implemented in the implementation part.
The class statement cant be nested ie we cant define a class within a class.
Instance attributes exists once per object ie once per runtime instance of the class. Syntax: DATA:
Static or Class attributes exists once per program run. Syntax: CLASS-DATA:
Methods can have a signature(interface parameters and exceptions). The implementation of the methods determines the "behavior" of the objects.
Objects can only be created and addressed using reference variables.
Definition of Instance methods : syntax: METHODS:
Definition of static methods: syntax : CLASS-METHODS.
Static methods are called using:
CALL METHOD class_name=>method_name EXPORTING IMPORTING CHANGING RECEIVING EXCEPTIONS. Or simply class_name=>method_name( EXP IMP CHN RECE EXCEP ).
Static attributes are accessed using classname=>static_attribute.
Instance attributes are accessed with ref->instance_attribute.
Reward points if useful.
Regards,
Sekhar
05-23-2008 11:30 AM
Hi,
Attributes : Internal data fields of class
Attributes can be either instance attributes u2013 specific to each instance of the class ( object ) or static attributes which are common to all instances
Methods :
Subroutines / procedures in a class that define the behavior of the object. Methods can also be instance methods or static methods
For defining static attributes we use key word u201CCLASS-DATAu201D and "DATA" for instance attributes.
For defining static methods use key word u201CCLASS-METHODu201D and "METHOD" fro instance methods.
reward if helpful.
regards,
Syed
05-23-2008 3:54 PM
HI Ranjna,
You can see an example on following link [ABAP Objects - Creating your First Local Class - Using Static Components|https://wiki.sdn.sap.com/wiki/x/ZtM].
For more examples see [ ABAP Objects Examples Beginner|https://wiki.sdn.sap.com/wiki/x/pto].
Greetings,
Marcelo Ramos
05-26-2008 11:21 AM
in short .
with class-data is same for class shared by all objects.
and normal data individual for each object..
cheers
05-26-2008 1:23 PM
Ranjana,
CLASS lcl_employee DEFINITION.
data: emp_no TYPE i,
emp_name TYPE c.
class-data:
no_of_employees type i.
ENDCLASS
Class describes the objects. That is, when we define a class we think of Object properties and methods.
As you see in the above example emp_no and emp_name are the object attributes, that is when you create 10 objects for that class, every object has its own emp_no and emp_name but there is class attribute called no_of_employees which does not belong to any object individually but to the class as a whole. When ever an object is created this variable gets incremented and is also called static variable.
In addition,
Memory gets allocated for emp_no and emp_name for every object created but for no_of_employee attribute, memory gets allocated when the class is referenced for the first time and persists as longs as the class is referenced.
Thanks & Regards,
Sujith
05-26-2008 6:12 PM
Hi Ranjana,
Seems you have left open all the queries even after getting the replies. Do close the queries for which you got answers by just marking the queries as "ANSWERED" instead of leaving the queries open in the forum. If you still doubts you can ask and get clarified and close the query. All the best.
Regards,
Sekhar
05-27-2008 6:02 AM
hi.
here class data is a static data .
here it is declared as class data but it is same as static data of c++.
here it can be declared once but it's scope is live through out program.
class method is like static function.
it can use class data only.
it can only by the use of class name only.
Reward if helpful.