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.


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

Results 1 to 3 of 3

Thread: How to get the java equivalent of C++ const

  1. #1
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    My Mood
    Thanked 25 Times in 25 Posts

    Cool How to get the java equivalent of C++ const

    How to you make sure that a variable passed to a method isn't altered by the method? I know in C++ you can do something like

    void aMethod(const Object &item)



    I know that you can stop a variable from being reinitialized in java by doing this

    void aMethod(final Object item)

    However, that won't stop it from calling a setter on the item or changing something in it.

    Is there some other keyword out there that can do this?

    Heck, I just found that java DOES recognize the const keyword but that it really is useless.

    I heard that you can possibly use an Accessor class to deal with that but that would be so grueling that it wouldn't be worth it as it would have to be for every class!!!!

    So, do you know of any practical way that const can be further approximated in java beyond using final?

  2. #2
    Senior Member
    Join Date
    Jul 2013
    Thanked 121 Times in 105 Posts

    Default Re: How to get the java equivalent of C++ const

    No. This is not possible in java and never will be.
    Just wrap your object up in another class to filter all mutations.
    You could also use typecasts and subclasses (or interfaces) which hide the setters.

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

    Wizand (June 12th, 2014)

  4. #3
    Junior Member
    Join Date
    Feb 2014
    My Mood
    Thanked 5 Times in 5 Posts

    Default Re: How to get the java equivalent of C++ const

    As Cornix said the Java way of making "const" is to build the class of the instance ( or wrap it in to one ) immutable by not giving out references to mutable variables, declaring the class final so it can be extended and removing setters.

    Here's Oracle's strategy for making class/object immutable:

    A Strategy for Defining Immutable Objects (The Java™ Tutorials > Essential Classes > Concurrency)

Similar Threads

  1. [SOLVED] Printing a Decimal Equivalent of a Binary Number
    By Arslan Ahmad 656 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 1st, 2014, 02:04 AM
  2. java equivalent of the following c# code
    By fzr600 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: April 7th, 2012, 05:28 PM
  3. Replies: 3
    Last Post: September 8th, 2011, 02:25 PM
  4. Java equivalent of Python’s struct.pack?
    By thiruvadi.e in forum Java SE APIs
    Replies: 2
    Last Post: July 14th, 2010, 03:09 AM
  5. Program to convert Hexadecimal to its Character equivalent
    By nathanernest in forum Java Theory & Questions
    Replies: 2
    Last Post: April 8th, 2009, 03:12 AM