final vs. const

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 1354 Views - Last Post: 18 September 2012 - 03:57 PM Rate Topic: -----

#16 healix  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 67
  • Joined: 29-May 11

Re: final vs. const

Posted 17 September 2012 - 11:03 PM

appreciated jon. Like I said I just started coding in Java so style advice is also helpful.

So basically you need a no=arg constructor in a class if you want to inherit from it?

I'm also not familiar with the super() constructor but I added a no-arg constructor and now it compiles fine.
Was This Post Helpful? 0
  • +
  • -

#17 ishkabible  Icon User is offline

  • spelling expret
  • member icon





Reputation: 1526
  • View blog
  • Posts: 5,514
  • Joined: 03-August 09

Re: final vs. const

Posted 18 September 2012 - 09:04 AM

just to explain this in terms of C++, 'const' used after a method declaration means that that method cannot modify 'this' and thus can be used from constant pointers and constant references. As far as I know Java doesn't have const correctness or const types so this idea doesn't even apply. you would just drop the const at the end.

also, 'final' applied to a method in java is similar to the default behavior of methods in C++. I won't go into the specifics but the 'virtual' keyword allows a C++ method to act more like a Java method.

This post has been edited by ishkabible: 18 September 2012 - 09:49 AM

Was This Post Helpful? 1
  • +
  • -

#18 DarenR  Icon User is online

  • D.I.C Lover

Reputation: 200
  • View blog
  • Posts: 1,525
  • Joined: 12-January 10

Re: final vs. const

Posted 18 September 2012 - 09:09 AM

just out of curiousity but why the conversion?
Was This Post Helpful? 0
  • +
  • -

#19 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 9029
  • View blog
  • Posts: 33,490
  • Joined: 27-December 08

Re: final vs. const

Posted 18 September 2012 - 09:42 AM

The const and goto keywords were carried over from C++. You get a nice slap on the wrist for using them though.
Was This Post Helpful? 1
  • +
  • -

#20 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 4882
  • View blog
  • Posts: 11,274
  • Joined: 16-October 07

Re: final vs. const

Posted 18 September 2012 - 10:07 AM

There's really no such thing as "const correctness" in Java. You use a method and there's never an explicit guarantee the state won't be changed.

The final for variables means you only get to assign it once. If it's object level, it must be in the constructor. In this sense, it's pretty much identical to a const variable declaration in a C++ class.

The final for method is different. It means subclasses don't get to override it. It's roughly the opposite of virtual in C++.

This post has been edited by baavgai: 18 September 2012 - 10:07 AM

Was This Post Helpful? 1
  • +
  • -

#21 healix  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 67
  • Joined: 29-May 11

Re: final vs. const

Posted 18 September 2012 - 03:57 PM

View PostDarenR, on 18 September 2012 - 09:09 AM, said:

just out of curiousity but why the conversion?


I'm learning java.

Thanks all for the info. Really helpful.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2