Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

const functions? 1

Status
Not open for further replies.

cpjust

Programmer
Sep 23, 2003
2,132
US
Hi, I'm kind of new to Java. I've been programming in C++ whenever possible. I'm starting to write some code in Java now and can't figure out how to create const functions.

In C++ you can declare a function with the const keyword after the function name, ex:
Code:
class SomeClass
{
public:
    void SomeFunc() const;
};
The const guarantees that the function will not modify the object's state (i.e. the member variables).

The closest equivalent I could find to const is the final keyword, but the description I read about final seems to indicate that it will only prevent derived classes from overriding that function...

Is there any equivalent in Java to making a function const?
 
Well said Tim, have you considered a career in Diplomacy ?

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Thanks sedj. I thought I'd try to make amends after my earlier grumpy reply.

Tim
 
I was actually hoping to get a little more experience with Java anyways since a lot more jobs now are looking for Java rather than C++... I was also thinking of learning .NET, but I'm just not sure how portable it will be to other platforms.

BTW, does anyone know if Java is standardized by any group like C & C++ are, or does Sun just add whatever they want to it? I don't like the idea of using proprietary languages.
 
Java does not have an ANSI standard.

Java at the end of the day is a proprietry language, owned by Sun - though people are free to implement their own JVMs (like kaffe or GCJ), though I would not recommend them to be honest, as they are usually buggy and several versions behind.



--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
But that doesn't mean Sun adds what it wants to.

In the end, they decide, but there is a so called Java-Community-Process (JCP) where you may participate in discussion of the Future of Java.

And a lot of things not developped by Sun went into the Java-libs, didn't it?

Formally Sun has control over Java, but defacto to many big Players depend on it, so Sun isn't free to anoy them by making curious steps.

A lot of libraries are available Open Source (reporting, databases, email, pdf-Generation, logging, junit-tests, ...).

I guess at least it is less proprietary than .NET.

seeking a job as java-programmer in Berlin:
 
Oh, the Java libraries are great! It comes fully loaded with just about everything you need for writing a program without having to reinvent the wheel...

It's just the language itself that I'm having a hard time getting used to, since I'm used to writing in C++ for so many years. Well that's not really true... With the huge number of libraries available it takes me find the right one. Most of the time I end up finding it after I've already written a similar class myself. Doh!

I see that Java has a lot of reserved words (like 'const'), but in all this time they still haven't implemented them. It kind of makes you wonder if they're ever going to use them?

Another thing I'd like to be able to do is specify whether the parameter my function takes will be passed by value or by reference (like the 'byval' and 'byref' keywords in C#).
 
You can't pass a parameter by value, nor by reference to a method.

You allways pass a copy of a reference.

You may pass a wrapped object (wrapped in an explicit created wrapper, or as element of a collection) to emulate pass-by-reference:
Code:
Foo foo = new Foo (17);
Foo[] afoo = {foo};

xy.pass (afoo);

class XY 
{
      void pass (Foo [] fooArray)
      {
           fooArray[0] = new Foo (42);
      }
}

I don't expect the const keyword to be used in future - especially not in conjuntion with methods.


seeking a job as java-programmer in Berlin:
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top