Questions about ALV object model

Hi,
for a new report i´m planing to use the "new" ALV object model to create the ALV list. Now I´ve got two questions concerning this topic:
- is it possible to switch the ALV into the edit mode like it´s possible if  the "old" CL_GUI_ALV_GRID class  
  is used?
- how I can encolor specific cells?
I couldn´t find any hints or demo programms for these questions
Regards,
Andy

it is not possible to Edit the ALV using Object Model.
For coloring...check this code.
DATA: alv TYPE REF TO cl_salv_table.
TYPES: BEGIN OF ty_tab,
         carrid TYPE sflight-carrid,
         connid TYPE sflight-connid,
         color  TYPE lvc_t_scol,
       END OF ty_tab.
DATA: wt_color TYPE  lvc_t_scol,
      wa_color TYPE  lvc_s_scol,
      w_color  TYPE  lvc_s_colo.
DATA: wa_flight TYPE ty_tab.
DATA: column_tab TYPE REF TO cl_salv_columns_table,
      column TYPE REF TO cl_salv_column_table.
DATA: column_ref TYPE   salv_t_column_ref,
      wa LIKE LINE OF column_ref.
DATA: it_flight TYPE STANDARD TABLE OF ty_tab.
SELECT carrid connid FROM sflight
INTO CORRESPONDING FIELDS OF TABLE it_flight
UP TO 10 ROWS.
w_color-col = 4.
w_color-int = 0.
w_color-inv = 0.
LOOP AT it_flight INTO wa_flight.
  w_color-col = 4.
  wa_color-fname = 'CARRID'.
  wa_color-color = w_color.
  APPEND wa_color TO wt_color.
  w_color-col = 6.
  wa_color-fname = 'CONNID'.
  wa_color-color = w_color.
  APPEND wa_color TO wt_color.
  wa_flight-color = wt_color.
  MODIFY it_flight FROM wa_flight.
ENDLOOP.
cl_salv_table=>factory(
  IMPORTING
    r_salv_table   = alv
  CHANGING
    t_table        = it_flight
"get all the columns
column_tab = alv->get_columns( ).
column_tab->set_color_column( value = 'COLOR' ).
column_ref = column_tab->get( ).
"loop each column
LOOP AT column_ref INTO wa.
  "Conditionally set the column type as key or non key
  IF wa-columnname   = 'CARRID'.
    column ?= wa-r_column.
    column->set_key( abap_true ).
  ENDIF.
ENDLOOP.
alv->display( ).

Similar Messages

  • Editable field in ALV Object Model

    Hi Guys,
    I'm using method cl_salv_table=>factory to display an ALV (Object Model) in my report.
    I want to "open up" one field for user entries, in other words make one column of my table editable.
    I assumed to find the respective methods in class cl_salv_table, cl_salv_columns_table or cl_salv_display_settings, so far without getting anywhere.
    I know about the field-catalogue and the method set_ready_for_input in class CL_GUI_ALV_GRID - it's not working once you're in the object model.
    Any ideas how to do that?
    Thank you!
    With best regards,
    Andreas

    Hi Guys,
    I'm using method cl_salv_table=>factory to display an ALV (Object Model) in my report.
    I want to "open up" one field for user entries, in other words make one column of my table editable.
    I assumed to find the respective methods in class cl_salv_table, cl_salv_columns_table or cl_salv_display_settings, so far without getting anywhere.
    I know about the field-catalogue and the method set_ready_for_input in class CL_GUI_ALV_GRID - it's not working once you're in the object model.
    Any ideas how to do that?
    Thank you!
    With best regards,
    Andreas

  • END_OF_PAGE in ALV Object model

    Dear all,
    I am using ALV Object model, but the END_OF_PAGE is not triggering. I have pasted the sample code below. Let me know how to get  END_OF_PAGE  in ALV OM.
    REPORT  ztest_rr.
    DATA : it_sflight TYPE TABLE OF sflight.
    DATA : gr_sflight TYPE REF TO cl_salv_table,
           gr_content TYPE REF TO cl_salv_form_element,
           gr_sorts   TYPE REF TO cl_salv_sorts,
           gr_events  TYPE REF TO cl_salv_events,
           gr_print   TYPE REF TO cl_salv_print.
          CLASS lcl_eventhandler DEFINITION
    CLASS lcl_eventhandler DEFINITION.
      PUBLIC SECTION.
        CLASS-METHODS:
          handle_top_of_page FOR EVENT
              top_of_page OF cl_salv_events
              IMPORTING
              page
              table_index
              r_top_of_page,
          handle_end_of_page FOR EVENT
              end_of_page OF cl_salv_events
              IMPORTING
              page
              r_end_of_page.
    ENDCLASS.                    "lcl_eventhandler DEFINITION
    DATA : gr_handler TYPE REF TO lcl_eventhandler.
          CLASS lcl_eventhandler IMPLEMENTATION
    CLASS lcl_eventhandler IMPLEMENTATION.
      METHOD handle_top_of_page.
        PERFORM create_alv_form_content_top
          USING    page
                   table_index
          CHANGING gr_content.
        r_top_of_page->set_content( gr_content ).
      ENDMETHOD.                    "handle_top_of_page
      METHOD handle_end_of_page.
        PERFORM create_alv_form_content_eop
        USING    page
        CHANGING gr_content.
        r_end_of_page->set_content( gr_content ).
      ENDMETHOD.                    "handle_end_of_page
    ENDCLASS.                    "lcl_eventhandler IMPLEMENTATION
    START-OF-SELECTION.
      SELECT *
      FROM sflight
      INTO TABLE it_sflight.
      TRY.
          CALL METHOD cl_salv_table=>factory
            EXPORTING
              list_display   = if_salv_c_bool_sap=>true
       r_container    =
       container_name =
            IMPORTING
              r_salv_table   = gr_sflight
            CHANGING
              t_table        = it_sflight
        CATCH cx_salv_msg .
      ENDTRY.
      CALL METHOD gr_sflight->get_sorts
        RECEIVING
          value = gr_sorts.
      CALL METHOD gr_sorts->set_group_active
        EXPORTING
          value = if_salv_c_bool_sap=>true.
      TRY.
          CALL METHOD gr_sorts->add_sort
            EXPORTING
              columnname = 'CARRID'
       position   =
       sequence   = IF_SALV_C_SORT=>SORT_UP
       subtotal   = IF_SALV_C_BOOL_SAP=>FALSE
               group      = if_salv_c_sort=>group_with_newpage
       obligatory = IF_SALV_C_BOOL_SAP=>FALSE
    receiving
       value      =
        CATCH cx_salv_not_found .
        CATCH cx_salv_existing .
        CATCH cx_salv_data_error .
      ENDTRY.
      CALL METHOD gr_sflight->get_event
        RECEIVING
          value = gr_events.
      CALL METHOD gr_sflight->get_print
        RECEIVING
          value = gr_print.
      CALL METHOD gr_print->set_reserve_lines
        EXPORTING
          value = 5.
      CREATE OBJECT gr_handler.
      SET HANDLER gr_handler->handle_top_of_page FOR gr_events.
      SET HANDLER gr_handler->handle_end_of_page FOR gr_events.
      CALL METHOD gr_sflight->display
    *&      Form  CREATE_ALV_FORM_CONTENT_TOP
          text
         -->P_PAGE  text
         -->P_TABLE_INDEX  text
         <--P_GR_CONTENT  text
    FORM create_alv_form_content_top  USING    l_page
                                               l_table_index
                                      CHANGING lr_content.
      WRITE 'HAI'.
    ENDFORM.                    " CREATE_ALV_FORM_CONTENT_TOP
    *&      Form  CREATE_ALV_FORM_CONTENT_EOP
          text
         -->P_PAGE  text
         <--P_GR_CONTENT  text
    FORM create_alv_form_content_eop  USING    l_page
                                      CHANGING lr_content.
      WRITE 'BYE'.
    ENDFORM.                    " CREATE_ALV_FORM_CONTENT_EOP

    Dear all,
    I am using ALV Object model, but the END_OF_PAGE is not triggering. I have pasted the sample code below. Let me know how to get  END_OF_PAGE  in ALV OM.
    REPORT  ztest_rr.
    DATA : it_sflight TYPE TABLE OF sflight.
    DATA : gr_sflight TYPE REF TO cl_salv_table,
           gr_content TYPE REF TO cl_salv_form_element,
           gr_sorts   TYPE REF TO cl_salv_sorts,
           gr_events  TYPE REF TO cl_salv_events,
           gr_print   TYPE REF TO cl_salv_print.
          CLASS lcl_eventhandler DEFINITION
    CLASS lcl_eventhandler DEFINITION.
      PUBLIC SECTION.
        CLASS-METHODS:
          handle_top_of_page FOR EVENT
              top_of_page OF cl_salv_events
              IMPORTING
              page
              table_index
              r_top_of_page,
          handle_end_of_page FOR EVENT
              end_of_page OF cl_salv_events
              IMPORTING
              page
              r_end_of_page.
    ENDCLASS.                    "lcl_eventhandler DEFINITION
    DATA : gr_handler TYPE REF TO lcl_eventhandler.
          CLASS lcl_eventhandler IMPLEMENTATION
    CLASS lcl_eventhandler IMPLEMENTATION.
      METHOD handle_top_of_page.
        PERFORM create_alv_form_content_top
          USING    page
                   table_index
          CHANGING gr_content.
        r_top_of_page->set_content( gr_content ).
      ENDMETHOD.                    "handle_top_of_page
      METHOD handle_end_of_page.
        PERFORM create_alv_form_content_eop
        USING    page
        CHANGING gr_content.
        r_end_of_page->set_content( gr_content ).
      ENDMETHOD.                    "handle_end_of_page
    ENDCLASS.                    "lcl_eventhandler IMPLEMENTATION
    START-OF-SELECTION.
      SELECT *
      FROM sflight
      INTO TABLE it_sflight.
      TRY.
          CALL METHOD cl_salv_table=>factory
            EXPORTING
              list_display   = if_salv_c_bool_sap=>true
       r_container    =
       container_name =
            IMPORTING
              r_salv_table   = gr_sflight
            CHANGING
              t_table        = it_sflight
        CATCH cx_salv_msg .
      ENDTRY.
      CALL METHOD gr_sflight->get_sorts
        RECEIVING
          value = gr_sorts.
      CALL METHOD gr_sorts->set_group_active
        EXPORTING
          value = if_salv_c_bool_sap=>true.
      TRY.
          CALL METHOD gr_sorts->add_sort
            EXPORTING
              columnname = 'CARRID'
       position   =
       sequence   = IF_SALV_C_SORT=>SORT_UP
       subtotal   = IF_SALV_C_BOOL_SAP=>FALSE
               group      = if_salv_c_sort=>group_with_newpage
       obligatory = IF_SALV_C_BOOL_SAP=>FALSE
    receiving
       value      =
        CATCH cx_salv_not_found .
        CATCH cx_salv_existing .
        CATCH cx_salv_data_error .
      ENDTRY.
      CALL METHOD gr_sflight->get_event
        RECEIVING
          value = gr_events.
      CALL METHOD gr_sflight->get_print
        RECEIVING
          value = gr_print.
      CALL METHOD gr_print->set_reserve_lines
        EXPORTING
          value = 5.
      CREATE OBJECT gr_handler.
      SET HANDLER gr_handler->handle_top_of_page FOR gr_events.
      SET HANDLER gr_handler->handle_end_of_page FOR gr_events.
      CALL METHOD gr_sflight->display
    *&      Form  CREATE_ALV_FORM_CONTENT_TOP
          text
         -->P_PAGE  text
         -->P_TABLE_INDEX  text
         <--P_GR_CONTENT  text
    FORM create_alv_form_content_top  USING    l_page
                                               l_table_index
                                      CHANGING lr_content.
      WRITE 'HAI'.
    ENDFORM.                    " CREATE_ALV_FORM_CONTENT_TOP
    *&      Form  CREATE_ALV_FORM_CONTENT_EOP
          text
         -->P_PAGE  text
         <--P_GR_CONTENT  text
    FORM create_alv_form_content_eop  USING    l_page
                                      CHANGING lr_content.
      WRITE 'BYE'.
    ENDFORM.                    " CREATE_ALV_FORM_CONTENT_EOP

  • ALV object model - List download

    H Experts,
    I have used ALV object model to list display.
    Also i have used class CL_ALV_COLUMNS_TABLE and its method set_long_text to set the column heading.
    Now if i execute this report, long text is displayed properly in column heading.
    But when i download this report output in spreadsheet, short text is displayed in column heading which is taken from the data element in DDIC.
    Example:
    I have declaed a column of type ABWTG.
    I have set leng text as 'TEST'.
    When i execute the report, column heading is displayed "TEST', which is as expected.
    But when i download this report in spreadsheet, column heading is displayed as 'NUMBER' in spread sheet. While downloading column heading is taken from the data element for field ABWTG. But i want the long text 'TEST' to be displayed when the report output is downloaded.
    Please suggest how to do it.
    Regards

    Hi,
    As per my understanding , it is displaying in excel based on ur ddic_output_length field.
    only becoz of that field it will show the same header.
    IF u will change the value at run time for this field then u will not get that issue,
    I m also searching on the same issue.
    Thanks
    Rahul

  • How do I create a context menu in the new ALV object model (cl_salv_table)?

    Hi,
    Does anyone know how to create a context menu (right click on line or field) in the new ALV object model (class CL_SALV_TABLE)?
    Thanks in advance
    Keld Gregersen
    PS: In the past we could use event CONTEXT_MENU_REQUEST in class CL_GUI_ALV_GRID, so it must be possible

    I don't think there's "any such animal" in the new class. I'm not 100% certain however but the new class is only useful for fairly simple display only type grids.
    There's no edit capability either.
    I'd stick with cl_gui_alv_grid until there's some decent extra functionality in the cl_salv_table class.
    It's fine for quick "bog standard" displays as it doesn't need a field catalog or any screens to be created by the user or developer  - but you pay a price for that in limited fnctionality.
    Cheers
    jimbo

  • ALV Object Model - Merged Cells

    Hi all
       When utilizing ALV Object Model, how do I display cells as merged when they have the same content?
       Can someone help me out?
       Thank you very much!

    Hello Yun
    In dialog cells are merged automatically as soon as you sort the column containing repeated values.
    Thus, when you call method SET_TABLE_FOR_FIRST_DISPLAY you should additionally provide parameter IT_SORT with the appropriate entries.
    For example, you want to sort your ALV list according to COL_A (1st) and COL_B (2nd):
    ls_sort-spos = 1.
    ls_sort-fieldname = 'COL_A'.
    ls_sort-up        = 'X'.  " sort ascending
    append ls_sort to lt_sort.
    ls_sort-spos = 2.
    ls_sort-fieldname = 'COL_B'.
    ls_sort-up        = 'X'.  " sort ascending
    append ls_sort to lt_sort.
    Regards
      Uwe

  • A question about ALV subtotal

    Hello Expert,
    I have a question about ALV subtotal
    In my ALV output, there are three columns: SalesOrder, Item, Sales Volumn. Suppose there are following data in the ALV output
    SalesOrder   Item   Volumn
    123              10      100.00
    123              20      300.00
    456              10      200.00
    456              20      500.00
    What I want in the ALV outpu is as below:
    SalesOrder   Item   Volumn
    123              10      100.00
    123              20      300.00
                     subtotal  400.00
    456              10      200.00
    456              20      500.00
                     Subtotal 700.00
    Total                       1100.00
    Is this possible, and how can I achieve that result?
    Thanks & Regards, Johnny

    You can use sortinfo structure of ALV.
      Data:GT_SORT TYPE slis_t_sortinfo_alv.
      GTT_SORT-FIELDNAME = 'VBELN'.
      GTT_SORT-UP = 'X'.
      GTT_SORT-SUBTOT = 'X'.
      APPEND GTT_SORT TO GT_SORT.

  • ALV  object  model examples

    Dear All
    Can anybody tell me, where will i find the examples for ALV report using object model (two-dimensional table).
    Thanks
    ravi

    Hi Ravindra,
    Transaction DWDM, Under grid controls...
    Also check out all report programs that contain BCALV_GRID*
    BCALV_GRID_DEMO being the simple one..
    CL_GUI_ALV_GRID is the class, you can find some sample programs by doing a where used list on it as well..
    Sri
    Message was edited by: Srikanth Pinnamaneni

  • About ALV tree model transaction

    Hi Every One..
       I got a requirement that we need to display some transactions in alv tree model. and if we click on that transaction then it should display that transaction in next half of custom container (like se80 transaction) .
      Please help me out, it's urgent..
    Thanks & Regards,
    Nagalakshmi

    Hi Nagalakshmi,
    Check these sample codes:
    BCALV_TREE_SIMPLE_DEMO
    BCALV_TREE_DEMO
    BCALV_GRID_DEMO
    BCALV_FULLSCREEN_DEMO_CLASSIC
    BCALV_FULLSCREEN_DEMO
    BCALV_DEMO_HTML
    BC_ALV_DEMO_HTML_D0100
    BCALV_TREE_SIMPLE_DEMO
    Reward If Useful.
    Regards,
    Chitra

  • Question about ObjectProxys and Models./

    Hi everyone!!
    first post
    I have an object proxy called mappingList which contains the name of a field as the label in a the matching models below (originModel and targetModel) and a value that contains a boolean of whether I should copy that fields value from the originModel to the targetModel.
    My question is, is there anyway i can use the string value of a property in one of the models to update the Model.   instead of originModel.firstName, i need to be able to do something like this:  originModel.setProperty("firstName")
    for (var p:String in mappingList){
         if(mappingList[p] = true){
              originModel. = targetModel;
    Is this possible?
    Thanks
    Neil

    Yeah this is one of those rare cases where right clicking a primative gives you more options.  From a interface prospective I like that this type of setting is quick to change and means less inputs to a subVI.
    From a developer perspecive I feel like they are hiding features behind menus.  Maybe if more functions supported right click options I would like it more, but as it is I think there are probably a dozen or so functions that do this.  Array to Cluster, Format Into String, Scan From String, Read/Write Text File, VISA Write/Read, Compound Arithmatic, In Range and Coerce, and several XNodes have extra menus too.  I probably missed some, but in the grand scheme of things where you have 100s of things on the palette, not many functions support this feature.
    Unofficial Forum Rules and Guidelines - Hooovahh - LabVIEW Overlord
    If 10 out of 10 experts in any field say something is bad, you should probably take their opinion seriously.

  • Questions about smart objects in CS3-CS5.

    Hi.
    Two questions.
    When I replace content of smart object in CS2 the content takes the size of previous state of smart object in pixels.
    It was very useful when need to replace some background to another and it autoresized to the new size and proportion.
    For example, when I replace content of smart object with external size 100x90px and 100x90% proportion (can see with Ctrl-T) with file with 200x300px size, i have new smart object with old size 100x90px and new proportion 50x30%.
    When I try to do the same in CS3-CS5 I get smart object with size 160x216px and proportion 100x90%.
    Haw can I return the old behaviour?
    Second.
    In CS2 I use files with a lot of smart objects each of them may consists with some others smart objects and so on.
    But i can`t use this scheme in CS3-CS5 because of very high consumption of scratch memory.
    Very-very simple example. In CS2 create file 4000x3000px, create new layer and draw there black square 300x300px.
    Convert the "square" layer to smart object. Then duplicate it with Ctlt-J 30 times. Save the file and reopen.
    The scratch size is about 280 MB and practically not changing when moving layers with movetool.
    Open this file in CS5. The scratch size is about 2.5 GB and quickly increase to 5 GB and above when i try to move layers. And this is a very simple file.
    If i will try to open several such files in CS5 the amount of useing memory will be fantastic.
    Why there is such behaviour whis memory in cs3-cs5 photoshops?
    PS. sorry for my english. hope you understand me.

    Hi,
    What version of Ps and AI are you using?
    1. In Ps, there's no controls for the AI layer visibilty, but the visibility attributes should be respected.
    2. The placed file is an embedded copy, not a link to an external file. However, there is a script that you might find useful for your purposes. I'm not sure if the Win support improved with CS5.
    http://ps-scripts.com/bb/viewtopic.php?t=3045%20Brilliant%21%21
    regards,
    steve

  • Question about using objects in SQL query.

    I had posted this question in the SQL/PLSQL forum but I guess nobody took the time to understand exactly what I am asking so I decided to try here hoping to get the answer. So here is the thing:
    I have created generic object type "tree" - the constructor takes as a parameter sql query which returns "node_id" and "parent_node_id" - this is all we need to have a tree. The object has all related to a tree structure member functions and one of them is "oldest_relative" (the tree may not be fully connected - it may be more like a set of many trees, so it's not necessary all nodes to have the same root).
    I also have departments table with the following fields: department_id, parent_department_id, department_name,...
    all records in the table w/out parent_departments (parent_department_id is null) are considered divisions.
    Now if I run the following query:
    SELECT "DEPARTMENT_ID", "PARENT_DEPARTMENT_ID", "DEPARTMENT_NAME", tree('select department_id "node_id", parent_department_id "parent_node_id" from departments').oldest_relative("DEPARTMENT_ID") "DIVISION_ID" FROM departments
    my question is: Is the tree object created for every row or does Oracle somehow caches the object since the object itself is not changing but only the parameter for the oldest_relative member function.
    The table only has a few hunderd records and I can't see much of a difference in the execution time btw the query above and query like this:
    SELECT "DEPARTMENT_ID", "PARENT_DEPARTMENT_ID", "DEPARTMENT_NAME", b.t.oldest_relative("DEPARTMENT_ID") "DIVISION_ID"
    FROM departments left join (select tree('select department_id "node_id", parent_department_id "parent_node_id" from departments') t from dual) b on 1 = 1
    where the object is clearly created just ones. (there is probably a better way to do it instead of this join)
    Pls elaborate
    George

    Not exactly sure what the question is...
    As I understand, you are comparing the following two constructor calls:
    +select..  tree('select department_id "node_id", parent_department_id "parent_node_id" from departments').oldest_relative("DEPARTMENT_ID") ... FROM ...+
    +select tree('select department_id "node_id", parent_department_id "parent_node_id" from departments') ... FROM dual+
    These calls are the same (besides the 1st one doing an immediate implicit call to a method of the object being constructed). The number of times these are being called depends on the number of times this SQL projection is applied - and that is determined by the number of rows being projected by the SELECT.
    The latter one is against DUAL which only has a single row. So that constructor is only called once. The former can be against multiple rows. Obviously a single pass through a data set is desirable - which means that the sub-select (use by the constructor) should ideally only be executed once and makes the 2nd method more desirable.
    However, I'm having a hard time understanding why the class and constructor are at all needed. Why pull data from a SQL table into PL memory? As that is where the class will need to cache and store the results of that construction parameter SQL SELECT. And once in PL memory, how does the object effectively access, search and use this cached data?
    PL memory is expensive. It is not sharable.
    PL data structures are primitive - these cannot be compared to SQL structures in the form of tables and columns that can be stored in a number of physical ways (index tables, hash tables, partitioned tables, clustered tables, etc). Cannot be indexed like SQL structures using B+tree, bitmap, function and other indexing methods. Cannot be sorted, grouped, analysed, filtered, etc like SQL structured data.
    It makes very little sense to read SQL data into a class and then deal with that data, cached in expensive PL memory, using primitive PL structures.
    And the same would be true if Java or C# was used. The best place for data is inside the SQL engine. That is the most superior environment for data. It can processes more data, scale better, perform better and offer more flexibility, than pulling data from it and then crunch that data using PL or Java or C#.

  • A question about immutable object: Integer

    import java.lang.Integer;
    public class ImmutableObject {
         public void changeImmutable(Integer x){
              x = x+1;
              System.out.print(x );          
    public static void main(String[] args){
         Integer x = new Integer(1);
         ImmutableObject i= new ImmutableObject();
         i.changeImmutable( x);
         System.out.print( x);     
    Hello everyone,
    Why the output of the program above is 21 rather than 22?
    Is it because of +? Can anyone tell me about this?
    Thanks a lot.
    Kolapig

    Isn't it call-by-reference? All parameters to methods are passed "by value." In other words, values of parameter variables in a method are copies of the values the invoker specified as arguments. If you pass a double to a method, its parameter is a copy of whatever value was being passed as an argument, and the method can change its parameter's value without affecting values in the code that invoked the method. For example:
    class PassByValue {
        public static void main(String[] args) {
            double one = 1.0;
            System.out.println("before: one = " + one);
            halveIt(one);
            System.out.println("after: one = " + one);
        public static void halveIt(double arg) {
            arg /= 2.0;     // divide arg by two
            System.out.println("halved: arg = " + arg);
    }The following output illustrates that the value of arg inside halveIt is divided by two without affecting the value of the variable one in main:before: one = 1.0
    halved: arg = 0.5
    after: one = 1.0You should note that when the parameter is an object reference, the object reference -- not the object itself -- is what is passed "by value." Thus, you can change which object a parameter refers to inside the method without affecting the reference that was passed. But if you change any fields of the object or invoke methods that change the object's state, the object is changed for every part of the program that holds a reference to it. Here is an example to show the distinction:
    class PassRef {
        public static void main(String[] args) {
            Body sirius = new Body("Sirius", null);
            System.out.println("before: " + sirius);
            commonName(sirius);
            System.out.println("after:  " + sirius);
        public static void commonName(Body bodyRef) {
            bodyRef.name = "Dog Star";
            bodyRef = null;
    }This program produces the following output: before: 0 (Sirius)
    after:  0 (Dog Star)Notice that the contents of the object have been modified with a name change, while the variable sirius still refers to the Body object even though the method commonName changed the value of its bodyRef parameter variable to null. This requires some explanation.
    The following diagram shows the state of the variables just after main invokes commonName:
    main()            |              |
        sirius------->| idNum: 0     |
                      | name --------+------>"Sirius"       
    commonName()----->| orbits: null |
        bodyRef       |______________|At this point, the two variables sirius (in main) and bodyRef (in commonName) both refer to the same underlying object. When commonName changes the field bodyRef.name, the name is changed in the underlying object that the two variables share. When commonName changes the value of bodyRef to null, only the value of the bodyRef variable is changed; the value of sirius remains unchanged because the parameter bodyRef is a pass-by-value copy of sirius. Inside the method commonName, all you are changing is the value in the parameter variable bodyRef, just as all you changed in halveIt was the value in the parameter variable arg. If changing bodyRef affected the value of sirius in main, the "after" line would say "null". However, the variable bodyRef in commonName and the variable sirius in main both refer to the same underlying object, so the change made inside commonName is visible through the reference sirius.
    Some people will say incorrectly that objects are passed "by reference." In programming language design, the term pass by reference properly means that when an argument is passed to a function, the invoked function gets a reference to the original value, not a copy of its value. If the function modifies its parameter, the value in the calling code will be changed because the argument and parameter use the same slot in memory. If the Java programming language actually had pass-by-reference parameters, there would be a way to declare halveIt so that the preceding code would modify the value of one, or so that commonName could change the variable sirius to null. This is not possible. The Java programming language does not pass objects by reference; it passes object references by value. Because two copies of the same reference refer to the same actual object, changes made through one reference variable are visible through the other. There is exactly one parameter passing mode -- pass by value -- and that helps keep things simple.
    -- Arnold, K., Gosling J., Holmes D. (2006). The Java� Programming Language Fourth Edition. Boston: Addison-Wesley.
    ~

  • Question about Using Objects

    Hi Guys,
    Need some help here with understanding the basics. Basically i'm getting a bit confused on how objects are instantiated and used in java. Firstly an object of a class is instantiated with the following piece of code;
    ClassA obj = new ClassA();This would allow you to use the variable and method of ClassA. Say ClassA has the method set(). You could call it via the obj object with the code;
    obj.set();that I understand. You can also declare an annonymous object which is an object that is only really going to be used once for the purposes of the statement. For example, something like;
    System.out.println(new Date());That I understand too. However what is confusing me is this type of object instantiation;
    public static ClassA obj; You cannot not call the set() method of the ClassA in the same way so why use this? Also this code is confusing me;
    obj = new ClassA();Are you saying that a precreated object is now equal to ClassA. Meaning that you can call its set() method in the same way?
    What do they mean? Why are they used? Can anyone give me an example of how they would be used?
    Any help would be appreciated.

    public static ClassA obj;This is a declaration, basically you are telling the
    computer that you intend to make a "ClassA" object
    and the program will allocate enough memory to hold a
    "ClassA" object, although the object "obj" does not
    actually "exist" at this time. This is why you can't
    use the set() method, because obj doesn't have any
    methods at all since it hasn't fully been created
    yet.
    obj = new ClassA();This is what actually creates the object, according
    to the instructions in the object's "ClassA()"
    method, which is known as a constructor.
    ClassA obj = new ClassA();This statement simply combines the two previous ones
    into one line. You are declaring "obj" to be a
    "ClassA" which allocates memory to store it, and then
    immediately executing the "ClassA()" constructor to
    build a new ClassA and store it in the memory space
    referenced by "obj". Does any of that make sense?
    I'm not exactly a teacher, but I think I understand
    your problem enough to explain it.Yes that does make sense. However i have some more questions now if you dont mind answering.
    If the code
        public static ClassA obj;simply allocates the memory (instantiate) and does not name (declare) the object then why cant you do something like this afterwards;
    obj = new ClassC();or this;
    obj1 = new ClassA();The object doesn't yet exist you only putting memory aside for it. Its only at this point that your naming the object and setting the parameters for it.
    Why bother even allocating memory for the object without actually declaring the object? Surely combing the two statements is more efficient and makes more sense than separating them. Why do you need to allocate the memory and then give it a name?
    Also the following piece of code;
    ClassA objA = obj;Since obj is already intialized as being a new ClassA object, then is this simply declaring and instantiating a new object of type ClassA? And what is the difference between this and;
    ClassA objA = new ClassA();Thank You

  • Another question about using objects in SQL queries

    Hi gurus, I need your thoughts on this:
    I have created generic object type "tree" - the constructor takes as a parameter sql query returning "node_id" and "parent_node_id". As a tree - the object has all related to a tree structure member functions and one of them is "oldest_relative" (the tree may not be fully connected - it may be more like a set of many trees, so it's not necessary all nodes to have the same root).
    I also have departments table with the following fields: department_id, parent_department_id, department_name,...
    all records in the table w/out parent_departments (parent_department_id is null) are considered divisions.
    Now if I run the following query:
    SELECT "DEPARTMENT_ID", "PARENT_DEPARTMENT_ID", "DEPARTMENT", tree('select department_id "node_id", parent_department_id "parent_node_id" from departments').oldest_relative("DEPARTMENT_ID") "DIVISION_ID" FROM departments
    my question is: Is the tree object created for every row or does Oracle somehow caches the object since the object itself is not changing but only the parameter for the oldest_relative member function.
    The table only has a few hunderd records and I can't see much of a difference in the execution time btw the query above and query like this:
    SELECT "DEPARTMENT_ID", "PARENT_DEPARTMENT_ID", "DEPARTMENT", b.t.oldest_relative("DEPARTMENT_ID") "DIVISION_ID"
    FROM departments left join (select tree('select department_id "node_id", parent_department_id "parent_node_id" from departments') t from dual) b on 1 = 1
    where the object is clearly created just ones. (there is probably a better way to do it instead of this join)
    Pls elaborate
    George

    Hi, TREE is not a function but PL/SQL object type I have written representing tree structure. The Oracle version is 10g.

Maybe you are looking for