CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2007
    Posts
    9

    Java doesn't have const, so how can I do this:

    in C++ I like to use the const keyword like this:

    private void method1() const {
    //do something, but don't modify the value of Class members
    }

    Is this possible in Java?

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Java doesn't have const, so how can I do this:

    Not really, no. In general, if you don't want a method to change class member values, you write it so it doesn't... and you can declare a method 'final' so it can't be overrridden in a subclass.

    It depends on your exact requirements, but there are a variety of ways to get similar effects with mutable and immutable instances, although they tend to involve subclasses or wrapper classes.

    Did you have some particular situation in mind?

    Programs must be written for people to read, and only incidentally for machines to execute...
    H. Abelson and G. Sussman
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  3. #3
    Join Date
    Jun 2007
    Posts
    9

    Re: Java doesn't have const, so how can I do this:

    No, it's just a habit that I developed as a C++ programmer. Disappointed that Java doesn't support this, but it could also save my time typing all those consts.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured