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 3 of 3

Thread: How to use final operator.

  1. #1
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Question How to use final operator.

    I had thought final in Java was like const in C++ sorta, or that was what I had wondered upon learning about const in C++.

    I had heard that you might not want a value to be changed and so passed a final object as a parameter and I tested it by changing the value and it let me.

    What's more, I tried making the whole method final and then calling it and using the returned (anonymous) object to call a set method and it let me do it.

    I thought "final" meant it can't be changed.

     
    /**
     * Write a description of class Example here.
     * 
     * @author (your name) 
     * @version (a version number or a date)
     */
    public class Example
    {
        // instance variables - replace the example below with your own
        private int x;
        private String words;
     
        /**
         * Constructor for objects of class Example
         */
        public Example(int x, String words)
        {
            setX(x);
            setWords(words);
        }
     
        public Example()
        {
     
        }
     
     
        public int getX()
        {
     
            return x;
        }
     
        public void setX(int x)
        {
            this.x = x;
        }
     
        public void setWords(String words)
        {
        this.words = words;
    }
     
     
    public String getWords()
    {
    return words;
    }
     
    // sets the values of this new Example object to the values of the parameter
    public final Example copy(  final Example ex)
    {
    Example ex2 = new Example(ex.getX(), ex.getWords());
    ex.setX(3);
    return (ex2);
    }
     
    public static void main(String[] args)
    {
    Example ex = new Example(5,"bob");
    Example ex2 = new Example(4, "mike");
    ex = ex.copy(ex2);
    ex.copy(ex2).setX(3);
    System.out.println(ex.getX());
    }
     
    }

    I had been trying to get it to see if there was a way in a clone method you could make it so the object you're cloning CAN'T be changed (by mistake) in the method.
    Last edited by javapenguin; September 9th, 2011 at 06:01 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How to use final operator.

    Final is a keyword (not an 'operator'), which means different things in different contexts. Suggested reading:
    final (Java) - Wikipedia, the free encyclopedia
    Do you have a specific question associated with this post?

  3. The Following User Says Thank You to copeg For This Useful Post:

    javapenguin (September 9th, 2011)

  4. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: How to use final operator.

    Żou answered my questions with that link.

Similar Threads

  1. Remainder Assignment Operator Help
    By jsam0123 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 5th, 2011, 06:04 PM
  2. Error with OR operator
    By tarkal in forum Loops & Control Statements
    Replies: 3
    Last Post: September 4th, 2011, 02:49 PM
  3. about Operator Precedence
    By degar in forum Java Theory & Questions
    Replies: 6
    Last Post: August 12th, 2011, 01:57 AM
  4. Operator Problem
    By KevinGreen in forum Algorithms & Recursion
    Replies: 2
    Last Post: November 2nd, 2009, 09:50 AM
  5. final class, final <variable> or <data member>
    By chronoz13 in forum Object Oriented Programming
    Replies: 9
    Last Post: September 20th, 2009, 08:19 AM