Passing objects as parameters

Hi folks,
I've got a general rather than a specific query here, and just need a bit of clarification as to whether or not I'm doing things right (I'm relatively new to Java).
Basically, I've set up several "sub classes" extending a parent class. In my code, I pass an object of a subclass into another class - this class then checks to see what subclass the object belongs to before doing some more fancy stuff. It's probably easier to explain if I show you basic code, so...
public ClassMain extends JFrame // or something
public  ClassMain(final ParentClass MyObject) // I have a question about this
If (MyObject instanceof SubClassA)
((SubClassA) myObject).setName("I'm an object of SubClassA");  // I have a question about this
If (MyObject instanceof SubClassB)
((SubClassB).setName("I'm an object of SubClassB");
// Obviously, I've printed this to the console elsewhere!
// etc. etc.
}Very simple stuff up there, I hope it explains what I'm trying to do. Anyway, my specific questions are:
(1) When I originally tried to compile my application, Eclipse threw an exception and advised that I add the "final" keyword when passing the object in. To be honest, I'm at a loss why I would NEED to declare the object as "final"...I've checked out books/online resources, and I seem to be missing an explanation. Even if I'm using "gets" instead of "sets" (i.e. I'm making no modification to the object variables), I still get the same error.
(2) While it works OK, this code:
((SubClassA) myObject).setName("I'm an object of SubClassA");  looks a bit wierd. It does work, but I'm convinced (I think I read it somewhere, but I've forgotten the source) there is a better way of writing this...can anyone let me know?
That's it for now. My application works and all, so no worries there...

Thanks all, that makes sense MLRon. Eclipse won't
compile your app AT ALL if you don't use "final" here
though.Yep, that's what the comment in my code said. It also stops you from saying:
public class MyClass
    int myVariable;
    public MyClass(int myVariable)
        myVariable = myVariable; // assigns it to itself.
}With "final", the above wouldn't compile. What is most likely intended by the above constructor is:
    public MyClass(int myVariable)
        this.myVariable = myVariable; // assigns instance variable
    }This second constructor WOULD compile with "final", and is most likely correct, where as the original is almost definitely an error.

Similar Messages

  • Passing Objects to Stored Procedures in SQL Queries: UNREF(REF(o))

    To me it seems that passing an object (type) to stored procedure in a SQL query is quite laborious. See:
    I created the following simple type:
    TYPE LAST AS OBJECT (
    id NUMBER
    defined a function:
    FUNCTION "CHECKLAST" (l LAST)
    RETURN NUMBER
    AS LANGUAGE JAVA
    NAME 'Checker.checkLast(Last) return int';
    created a typed table:
    CREATE TABLE lastTable OF Last;
    inserted some objects:
    INSERT INTO lastTable VALUES (1003);
    NOW! If want to use the StoredFunction I have to use the following syntax:
    SELECT checkLast(DEREF(REF(o)))
    FROM lasttable o;
    That's not very elegant.
    I would expect the follwing to be working:
    SELECT checkLast(o)
    FROM lasttable o;
    Did I do something wrong? Or did Oracle forget to implement the simple syntax?
    Even worse, the DEREF(REF(o)) trick does not work for member functions...
    Regards,
    André

    I did some experiments and can now (partly) answer my own question:
    Indeed, it is possible to declare a function with a parameter passed as reference:
    CREATE OR REPLACE TYPE Point AS OBJECT
    ID INT,
    MEMBER FUNCTION distance(q IN REF Point) RETURN NUMBER
    Unfortunately, it is not possible to use the REF in PL/SQL directly:
    CREATE OR REPLACE TYPE BODY Point is
    MEMBER FUNCTION distance(q IN REF Point) RETURN NUMBER
    AS
    BEGIN
    RETURN q.ID - SELF.ID; --Error(5,14): PLS-00536: No navigation through REF
    END;
    end;
    So, for testing purposes I decided to implement a very simple PL/SQL member function:
    CREATE OR REPLACE TYPE BODY Point is
    MEMBER FUNCTION distance(q IN REF Point) RETURN NUMBER
    AS
    BEGIN
    RETURN -1;
    END;
    end;
    BUT: The SQL query without VALUE is still not possible. The query
    SELECT *
    FROM PointTable d, PointTable e
    WHERE d.distance(e) > 0
    renders ORA-00904: "E": invalid identifier
    Somehow the 'e' seems to be misinterpreted completely.
    Illogically, the following query works fine:
    SELECT *
    FROM PointTable d, PointTable e
    WHERE d.distance(REF(e)) > 0
    The illogical thing is that, now 'E' seems to refer to a VALUE and not to REF (because we can use the REF function), whereas in Gaverill's post 'O' seems to refer to a REF (because we can use the VALUE function).
    My intent is to avoid both the VALUE(o) from Gaverill's example and the REF(e) from my example, because both is not intuitive for end users...
    Maybe I have to define a VIEW holding the object values... But then the end user has to use it in the FROM part.
    All in all, there seems to be no elegant solution for passing objects as parameters to functions...
    Regards,
    André

  • Passing Object parameters to Procedures in Oracle

    Hi,
    Can any one please provide me with a sample example how to pass object types as parameters (IN) to a procedure/package.
    Thanks in Advance.

    Here is a simple example
    SQL> create or replace type tbl as table of number
      2  /
    Type created.
    SQL> create or replace procedure proc(pTbl tbl)
      2  as
      3  begin
      4     for i in 1..pTbl.count
      5     loop
      6             dbms_output.put_line(pTbl(i));
      7     end loop;
      8  end;
      9  /
    Procedure created.
    SQL> exec proc(tbl(1,2,3,4,5))
    1
    2
    3
    4
    5
    PL/SQL procedure successfully completed.

  • Passing objects by reference in PL/SQL

    Hi,
    I have come across an unexpected problem using object types in PL/SQL that is causing me some grief. I'm from a Java background and am relatively new to Oracle Objects but what I'm trying to do is fairly trivial, I think. The code below illustrates the problem.
    --- cut here ---
    CREATE OR REPLACE TYPE test_obj_t AS OBJECT
    num INTEGER,
    CONSTRUCTOR FUNCTION test_obj_t RETURN SELF AS RESULT
    CREATE OR REPLACE TYPE BODY test_obj_t IS
    CONSTRUCTOR FUNCTION test_obj_t RETURN SELF AS RESULT IS
    BEGIN
    num := 0;
    RETURN;
    END;
    END;
    CREATE OR REPLACE PACKAGE test_obj_ref AS
    PROCEDURE init(o IN test_obj_t);
    PROCEDURE inc;
    FUNCTION get_num RETURN INTEGER;
    END;
    CREATE OR REPLACE PACKAGE BODY test_obj_ref IS
    obj test_obj_t;
    PROCEDURE init(o IN test_obj_t) IS
    BEGIN
    obj := o;
    END;
    PROCEDURE inc IS
    BEGIN
    obj.num := obj.num + 1;
    END;
    FUNCTION get_num RETURN INTEGER IS
    BEGIN
    RETURN obj.num;
    END;
    END;
    --- cut here ---
    The object type test_obj_t holds a integer and the test_obj_ref package holds a 'reference' to an instance of the object.
    To test the above code I run this PL/SQL block:
    declare
    obj test_obj_t;
    begin
    obj := test_obj_t;
    test_obj_ref.init(obj);
    dbms_output.put_line('obj.num='||obj.num);
    dbms_output.put_line('test_obj_ref.get_num='||test_obj_ref.get_num);
    test_obj_ref.inc;
    dbms_output.put_line('obj.num='||obj.num);
    dbms_output.put_line('test_obj_ref.get_num='||test_obj_ref.get_num);
    test_obj_ref.inc;
    dbms_output.put_line('obj.num='||obj.num);
    dbms_output.put_line('test_obj_ref.get_num='||test_obj_ref.get_num);
    end;
    giving the output:
    obj.num=0
    test_obj_ref.get_num=0
    obj.num=0
    test_obj_ref.get_num=1
    obj.num=0
    test_obj_ref.get_num=2
    It appears that the object held by the test_obj_ref package is being incremented as expected, but I would have expected the object declared in the PL/SQL block to be pointing to the same object and so should report the same incremented values.
    I suspect that the object is copied in the call to test_obj_ref.init() so I end up with two object instances, one that is held by the test_obj_ref package and one in the anonymous block. Although, I thought that all IN parameters in PL/SQL are passed by reference and not copied!
    Am I right?
    Is passing objects by reference possible in PL/SQL, if so how?
    I'm using Oracle 10.2.0.3.
    Cheers,
    Andy.

    the object being passed to the test_obj_ref.init+ procedure is passed by reference; however, when you assign it to your package variable obj it is being copied to a new instance. you can pass object instances as parameters to procedures using the +IN OUT [NOCOPY]+ *calling mode, in which case modifications to the attributes of the passed object will be reflected in the calling scope's instance variable.
    oracle's only other notion of an object reference is the +"REF <object-type>"+ datatype, which holds a reference to an object instance stored in an object table or constructed by an object view.
    hope this helps...
    gerard

  • Can we pass objects to pl/sql block

    hi,
    can we pass objects to pl/sql block.i think we can.how to pass it.help me in getting the examples of "passing objects to pl/sql block"

    What exactly do you mean ? You can pass objects like any other parameters
    into and out from procedures/functions:
    SQL> create or replace procedure get_object(myobj in out nocopy my_obj)
      2  is
      3  begin
      4   dbms_output.put_line('ID : ' || myobj.empno);
      5   dbms_output.put_line('Salary : ' || myobj.sal);
      6   dbms_output.put_line('Department : ' || myobj.deptno);
      7   myobj.sal := myobj.sal*2;
      8  end;
      9  /
    Procedure created.
    SQL> declare
      2   mo my_obj := my_obj(1,1000,10);
      3  begin
      4   get_object(mo);
      5   dbms_output.put_line('New salary : ' || mo.sal);
      6  end;
      7  /
    ID : 1
    Salary : 1000
    Department : 10
    New salary : 2000
    PL/SQL procedure successfully completed.Rgds.

  • Non-string objects as parameters to an applet

    how can i pass non-string objects as parameters to an applet?
    thanks in advance...

    those are some objects that i wroteThen, you could use Class.forName() method for your string parameters.
    If you get the classes, then you can call newInstance() method on them to get objects.
    Alternative way could be using your string parameter as index or keys for some
    data structures that contains ready-made objects.

  • Passing objects to a applet.

    hi there,
    i have a jsp and a applet. i need to pass an object from my jsp to the applet and the applet will present the results.
    how can i do it?? how do i pass objects to the applet??
    thanks

    you typically pass strings as applet parameters in the page.
    otherwise, you have your applet connect back to the server with a socket or URLConnection to a servlet, which can send serialized objects if that's what you need, or other data.

  • Pass object to xslt stylesheet and invoke its methods

    I'd like to pass an external created object to a xslt stylesheet to dynamically modify the xslt file at run time. After searching around for weeks, I'm really desperate.
    I used Xalan transformer's method setParameter(name, obj) to initialize a variable in xslt file with this object. Then the object's method was invoked.
    The class that I want to invoke the method:
    class test{
    private String testString = "abc";
    public String valueOf(){
    return testString;
    xslt file:
    <xsl:param name="myType"></xsl:param>
    <<xsl:variable name="new-pop"
    select="my-class:valueOf($myType)">
    Any help is greatly appreciated.
    Thank you.
    Message was edited by:
    Orbital
    Message was edited by:
    Orbital

    Thank sabre. I have looked through your link.
    The problem is for all the info I knew, we can only
    create a new object inside the stylesheet using new()
    and then invoke this particular object's instance
    method.
    However, I want to pass an already created java
    object into the stylesheet and then invoke its
    method.
    Xalan seems to not allow this. I have tried to pass
    an object as the parameter of
    transformer.setParameter(name, object) but it doesn't
    work.
    Any one know what 3rd party transformer that allow to
    pass object directly into xslt?setParameter will work... in your XSL, you should have
    <xsl:param name="myParam" />set the parameter in your transformer like what you had in your post...
    In your XSL header, you must declare the your Java object namespace and path, such as:
    xml:myJavaObject= "com.MyCompany.MyJavaObject"then in your template or anywhere that you want to use your object, you should have:
    <xsl:variable name="runningMyMethod" select="myJavaObject:myJavaMethod($myParam)" />The XSL will treat $myParam as the instance object, if there is any other method parameters needed to be passed in do:
    <xsl:variable name="runningMyMethod" select="myJavaObject:myJavaMethod($myParam, 'blah', 'blah')" />Good luck.

  • Passing object as parameter in JSF using h:commandLink tag

    Hi ,
    I have a scenario in which i need to pass objects from one jsp to another using h:commandLink. i read balusC article "Communication in JSF" and found a basic idea of achieving it. Thanks to BalusC for the wonderful article. But i am not fully clear on the concepts and the code is giving some error. The code i have is
    My JSP:
    This commandlink is inside a <h:column> tag of <h:dataTable>
    <h:commandLink id="ol3"action="ManageAccount" actionListener="#{admincontroller.action}" >
                   <f:param id="ag" name="account" value="#{admincontroller.account}" />
                   <h:outputText id="ot3" value="Manage Account" rendered="#{adminbean.productList!=null}" />
                   </h:commandLink>
    Also a binding in h:dataTable tag with the  binding="#{admincontroller.dataTable}"
                      My Backing Bean:
    public class CompanyAdminController {
              private HtmlDataTable dataTable;
              private Account account=new Account();
    public HtmlDataTable getDataTable() {
            return dataTable;
    public Account getAccount() {
             return this.account;
    public void setAccount(Account account) {
              this.account = account;
          public void action(){
                account= (Account)this.getDataTable().getRowData();
           } faces-config.xml
    <navigation-rule>
        <from-view-id>/compadmin.jsp</from-view-id>
        <navigation-case>
          <from-outcome>ManageAccount</from-outcome>
          <to-view-id>/manageAccount.jsp</to-view-id>
        </navigation-case>
      </navigation-rule>
    <managed-bean>
              <managed-bean-name>admincontroller</managed-bean-name>
              <managed-bean-class>com.forrester.AdminController</managed-bean-class>
              <managed-bean-scope>request</managed-bean-scope>
              <managed-property>
                   <property-name>account</property-name>
                   <property-class>com.model.Account</property-class>
                   <value>#{param.account}</value>
             </managed-property>
         </managed-bean>My account object:
    public class Account {
    string name;
      public String getName()
        return this.name;
      public void setName(String name)
           this.name=name;
      }I need to display #{admincontroller.account.name} in my forwarded jsp. But I am not sure whether the code i wrote in commandlink is correct. I get an error if i use <f:setActionListener> . No tag "setPropertyActionListener" defined in tag library imported with prefix "f"
    Please advise.
    Edited by: twisai on Oct 18, 2009 11:46 AM
    Edited by: twisai on Oct 18, 2009 11:47 AM
    Edited by: twisai on Oct 18, 2009 11:48 AM

    Yes.. iam removed the managedproperty from faces-config. But still i get the null pointer exception on main jsp itself and i am taken to second jsp manageaccount.jsp. Did you find anything wrong in my navigation case in the action method??
                                     public String loadAccount(){
               Account account = (Account)this.getDataTable().getRowData();
                return "ManageAcct";And also can you please answer this question i have
    The AdminController is the backing bean of 1st JSP and manageAccontController is the backing bean of forwarded JSP .
    Can i put this action method and dataTable binding in a 2nd manageAccontController backing bean instead of AdminController. But if i do that way dataList binding in h:dataTable tag will be in first JSP backing bean and dataTable binding to 2nd JSP. Not sure how to deal with this.
    {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

  • To pass the page parameters to a form in portal

    Hi,
    I am trying to develope a simple page using portal which contains
    1. Simple parameter form.
    2. Form that accepts the data from the simple parameter form.
    I want to pass 2 page parameters to this form(no.2)
    Is it possible to pass the page parameters to a form in portal?

    You can see a simple example of setting parameters here:
    http://www.patrickhaston.co.uk/Portal/parameters.html
    And to use them in a form:
    http://www.patrickhaston.co.uk/Portal/webplsqlapp1.html
    Hope this helps.
    Patrick

  • How to pass the report parameters through java not by using URL

    Hello...
    I have an oracle App. Server 10g with report service
    I can the report using the URL :
    http://host:Port/rwservlet/report=....
    and passing the report parameters ...
    But is there any way to call the report by pdf format and passing the parameters from java without using the url ???

    thank you shahcsanjay for your reply
    but I think that web.showDocument can not be used by ordinary java web application ..
    I think it can be used only with with "oracle forms" Am I right ?
    If no can you please tell me where can I find a useful document about how to use web.showDocument ...
    thanks again
    Saleem

  • Calling procedures multiple times passing rum time parameters

    hii,
    i ve this small prob with my procedure..my proc is
    CREATE or replace PROCEDURE sad(star_val number,dept_id number) IS
    BEGIN
    update dept set star=star_val where department_id=dept_id;
    END;
    and i call this proc using a loop and give the values at runtime..
    declare
    v_count number(3);
    begin
    v_count := 4;
    loop
    sad(&value,&dept);
    v_count := v_count-1;
    exit when v_count=0;
    end loop;
    end;
    this procedure runs only once with out looping 3 times..can anybody help me out..

    .... passing rum time parameters ......
    programming in the caribbeans must be wonderful
    anyway : how do you know it ran only once ?
    most probably it ran 5 times doing the same update defined by &value,&dept
    ahh, now I get it, you believe you'll be asked again and again to enter values for &value,&dept .
    Sorry, no can do, PLSQL is not interactive
    further reading at http://tahiti.oracle.com

  • Memory leak problem while passing Object to stored procedure from C++ code

    Hi,
    I am facing memory leak problem while passing object to oracle stored procedure from C++ code.Here I am writing brief description of the code :
    1) created objects in oracle with the help of "create or replace type as objects"
    2) generated C++ classes corresponding to oracle objects with the help of OTT utility.
    3) Instantiating classes in C++ code and assigning values.
    4) calling oracle stored procedure and setting object in statement with the help of setObject function.
    5) deleted objects.
    this is all I am doing ,and getting memory leak , if you need the sample code then please write your e-mail id , so that I can attach files in reply.
    TIA
    Jagendra

    just to correct my previous reply , adding delete statement
    Hi,
    I am using oracle 10.2.0.1 and compiling the code with Sun Studio 11, following is the brief dicription of my code :
    1) create oracle object :
    create or replace type TEST_OBJECT as object
    ( field1 number(10),
    field2 number(10),
    field3 number(10) )
    2) create table :
    create table TEST_TABLE (
    f1 number(10),f2 number (10),f3 number (10))
    3) create procedure :
    CREATE OR REPLACE PROCEDURE testProc
    data IN test_object)
    IS
    BEGIN
    insert into TEST_TABLE( f1,f2,f3) values ( data.field1,data.field2,data.field3);
    commit;
    end;
    4) generate C++ classes along with map file for database object TEST_OBJECT by using Oracle OTT Utility
    5) C++ code :
    // include OTT generate files here and other required header files
    int main()
    int x = 0;
    int y = 0;
    int z =0;
    Environment *env = Environment::createEnvironment(Environment::DEFAULT);
    Connection* const pConn =
    env->createConnection"stmprf","stmprf","spwtrgt3nms");
    const string sqlStmt("BEGIN testProc(:1) END;");
    Statement * pStmt = pConn->createStatement(sqlStmt);
    while(1)
    TEST_OBJECT* pObj = new TEST_OBJECT();
    pObj->field1 = x++;
    pObj->field2 = y++;
    pObj->field3 = z++;
    pStmt->setObject(1,pObj);
    pStmt->executeUpdate();
    pConn->commit();
    delete pObj;
    }

  • Hoe to pass object as a parameter thru webSerive

    I need to pass Object as a parameter to web service:
    example:
    public calss returnCodes{
    int ret_code;
    String ret_string;
    public retrunCodes(int ret_code, String ret_string){
    this.ret_code = ret_code;
    this.ret_string = ret_string;
    public int getret_code()
    return this.ret_code;
    public String getret_string {
    return this,ret_string
    and other repc holder class to hold returnCodes
    public class retrunCodesHolder implents javax.xml.rpc.Holder
    returnCode value;
    pubic returnCodesHolder(){
    public returnCodesHolder(returnCodes value) {
    this.value = value;
    In Webservice I use returnCodesHolder as parameter.
    WebService getReturnCodesWs I have
    returnCodes retCodes = new returnCodes(04,"Good return code)
    returnCodesHolder retholder = new returnCodesHolder(retCodes)
    return myString;
    When I am calling above webService in servlet I have code:
    org.test.mytest.returnCodesHolder retHolder = new org.test.mytest.returnCodesHolder();
    String result = port.getReturnCodesWs(retHolder);
    How to I access /get values of returnCodes class in my calling servlet?
    I am using Tomcat v 6.0

    I believe (although have never used, so if I'm wrong,
    correct me please!) you can do it with implicit
    dynamic cursors, such as:
    For rec in 'select col1, col2 from table 1 '||lstr
    loop
    Not really:
    michaels>  declare
       lstr   varchar2 (30) := ' where empno = 7788';
    begin
       for rec in 'select * from emp e ' || lstr
       loop
          null;
       end loop;
    end;
    Error at line 1
    ORA-06550: line 5, column 4:
    PLS-00103: Encountered the symbol "LOOP" when expecting one of the following:
       . ( * @ % & - + / at mod remainder rem .. <an exponent (**)>
       ||

  • Passing Runtime initialization parameters to a Servlet

    Hi all.
    Plz.let me know, how do you pass runtime initialization parameters to a Servlet. Also, when do we need this approach. Thank You.

    Global parameters that may change should be passed as parameters so that the servlet doesn't have to be recompiled to make the change.
    Here a global parameter is used to store a timeOut value. This value is written to every web page to force the page to refresh.
    <web-app>
        <servlet>
            <servlet-name>
                KioskServlet
            </servlet-name>
            <servlet-class>
                KioskServlet
            </servlet-class>
            <init-param>
                <!-- number of seconds of inactivity until timing out -->
                <!-- 300 = 5 minutes -->
                <param-name>timeoutValue</param-name>
                <param-value>300</param-value>
            </init-param>
         </servlet>
    </webapp>

Maybe you are looking for