Results 1 to 4 of 4
  1. #1
    snooze-g is offline Member
    Join Date
    Jul 2007
    Posts
    8
    Rep Power
    0

    Exclamation Function declaration problem.

    Hi all.
    Imagine that wi have an interface like this
    Java Code:
    public Interface MyInterface 
    {
    
    public void function1(Object a);
    
    
    
    }
    how i must write my function definition to be sure that the function1 doesn't change my Object a during his work.

    Best Snooze-G.

  2. #2
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    9

    Default

    What about this? Is it working?

    Java Code:
    public Interface MyInterface 
    {
    
    public void function1(final Object a);
    
    }

  3. #3
    snooze-g is offline Member
    Join Date
    Jul 2007
    Posts
    8
    Rep Power
    0

    Default

    No, of course it doesn't work, because Java work in referance system, for Java it mean that the referance of "Object a" can't be changed, not content but the referance.
    For example.
    Java Code:
    public void function1(final Object a)
    {
     1. a=new Object();
     2. a.text="new text";
    }
    It wil get a compiler time error on line 1, but not on line 2.

    Best Snooze-G.
    Thanks.
    Last edited by snooze-g; 07-18-2007 at 09:55 PM.

  4. #4
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    9

    Default

    That is right. Then you will design your Object such a way that method can not change its content.

    For example, define an interface which will only have the method you want to be called from this method and send this method the interface instead of actual object.

    Of course the method can try casting the object to its actual type, but if you really want to protect that object then only way is to not sending that object obviously :)

Similar Threads

  1. Declaration
    By asifahmed in forum New To Java
    Replies: 1
    Last Post: 04-05-2008, 06:38 AM
  2. multiple declaration of a variable
    By eva in forum New To Java
    Replies: 2
    Last Post: 01-28-2008, 10:17 AM
  3. Array size declaration
    By JT4NK3D in forum New To Java
    Replies: 3
    Last Post: 01-18-2008, 11:37 PM
  4. JSP Declaration Directive
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-10-2007, 06:42 PM
  5. problem with Vectors and getTotal() function
    By java_fun2007 in forum New To Java
    Replies: 2
    Last Post: 11-23-2007, 02:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •