Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6793463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:06:13+00:00 2026-05-26T18:06:13+00:00

Groovy noob here, I’m working through my first Groovy book and it has example

  • 0

Groovy noob here, I’m working through my first Groovy book and it has example code where it states roughly

“If I want a property to be a ready-only property then declare it final. This is not defining a final field but a read-only property-you can change the property from within instance methods of the defining class, but not from outside”

Here is the code I have in question, but I keep getting an error stating:

cannot modify final field 'miles' outside of constructor.

Code:

class Car
{
    final miles = 0

    def getMiles()
    {
        println "getMiles called"
        miles
    }

    def drive(dist){if (dist>0) miles += dist }

}

The book says I should be able to modify miles from within the drive instance method, am I doing something wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T18:06:14+00:00Added an answer on May 26, 2026 at 6:06 pm

    I think what they meant (not sure what they said, if you’re paraphrasing) is that there’s no setter method defined, so it can’t be modified from outside the class.

    It is, however, still a final property, which means it can’t be modified once its set, which would be in a constructor or during the declaration.

    Property and field rules

    That said, see these two issues: 1628, 2752, so more exploration might be necessary, although this appears limited to local script properties.

    My guess is you’re using a 1.7+ Groovy, while the book targets <= 1.6.

    See also this SO question.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am a total Groovy newbie. I saw the following code here . def
Groovy has a concept of GStrings. I can write code like this: def greeting
I started dabbling in groovy yesterday. There's an example on the groovy website that
File1.groovy def method() { println test } File2.groovy method() I want to load/include the
In Groovy code something simple: #!/usr/bin/env groovy public class test { boolean val def
In Groovy, I can write code such as: assert 1 in [1,2,3] assert foo
I'm using groovy scripting in java. Here is how it operates: I'm creating in
i have a groovy application, i want to store a value to re use
This is my Groovy code: // ... def xml = new XmlParser().parse(fileName) xml.each {
I'm following a groovy tutorial and there is a code like this: def fruit

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.