Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: How to protect a parameter beeing modified

  1. #1
    Junior Member
    Join Date
    Sep 2017
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to protect a parameter beeing modified

    Hi,

    I'm a total beginner with Java - coming from C++
    In the code snippet below, I want to protect the parameter 'V" in method display
    from beeing modified. How can this be achieved (the attribute 'final" doesn't seem to work).
    Many thanks for a hint,
    Helmut.

    class Test {
    public static void main(String[] args) {
    int[] A = new int[3];
    for (int i=0; i < A.length;i++) A[i]= 7*i;
    display(A);
    System.out.println(A[0]);
    }

    static void display( final int[] V) {
    V[0]=17; // WHY NO ERROR here <========================== ?
    for (int i=0; i < V.length;i++)
    System.out.println(i+" "+V[i]);
    }
    }

  2. #2
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    277
    My Mood
    Amused
    Thanks
    8
    Thanked 19 Times in 19 Posts

    Default Re: How to protect a parameter beeing modified

    What error you expect here ?
    Whatever you are, be a good one

  3. #3
    Junior Member
    Join Date
    Sep 2017
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to protect a parameter beeing modified

    Quote Originally Posted by John Joe View Post
    What error you expect here ?
    I thought that declaring the parameter V as final would make any assignments to any element of the array V illegal.
    If this does not work how can I protect a parameter of a method from being modified?
    In C++ I would declare this parameter as
    const Array& V
    which would make any assignments to any element of V an error at compile time.
    Helmut

  4. #4
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    277
    My Mood
    Amused
    Thanks
    8
    Thanked 19 Times in 19 Posts

    Default Re: How to protect a parameter beeing modified

    Have a looked on this : https://stackoverflow.com/questions/...utable-in-java
    final has no effect on that
    Whatever you are, be a good one

  5. #5
    Junior Member
    Join Date
    Sep 2017
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to protect a parameter beeing modified

    Quote Originally Posted by John Joe View Post
    Have a looked on this : https://stackoverflow.com/questions/...utable-in-java
    final has no effect on that
    Many thanks for explaining this.

    FLAMES ON
    But let me say that this is fundamental flaw of Java.
    The same is with the lack of "constant" or "final" methods.
    /FLAMES OFF

  6. #6
    Member John Joe's Avatar
    Join Date
    Jun 2017
    Posts
    277
    My Mood
    Amused
    Thanks
    8
    Thanked 19 Times in 19 Posts

    Default Re: How to protect a parameter beeing modified

    So glad you found the solution
    Whatever you are, be a good one

Similar Threads

  1. protect my java application
    By wardafash in forum Java Theory & Questions
    Replies: 5
    Last Post: May 29th, 2013, 11:38 AM
  2. [SOLVED] iam not beeing printed to screen
    By hwoarang69 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 30th, 2012, 02:06 PM
  3. Replies: 3
    Last Post: May 27th, 2012, 11:11 AM
  4. Updation of webpage to the last modified date
    By Revathy G in forum Java Programming Tutorials
    Replies: 1
    Last Post: May 6th, 2009, 04:47 AM
  5. Updation of webpage to the last modified date
    By Revathy G in forum Java Code Snippets and Tutorials
    Replies: 1
    Last Post: May 6th, 2009, 04:47 AM