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 8754345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:35:58+00:00 2026-06-13T13:35:58+00:00

class GroovyClass { def aVariable void setAVariable(aVariable) { this.aVariable = aVariable; } } My

  • 0
class GroovyClass {
  def aVariable

  void setAVariable(aVariable)
  {
      this.aVariable  = aVariable;
  }
}

My understanding was that we don’t need to specify the type of a variable in a groovy class. But Groovy compiler complains if I declare ‘aVariable’ , why isn’t it considered as a typeless variable with default accessibility ? Should every variable be defined with a def in Groovy both local and class ? Why is it that the function definition doesn’t have to begin with a def ? and when I’m passing in a variable to the setter, it doesn’t need any def in there ?

  • 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-06-13T13:35:59+00:00Added an answer on June 13, 2026 at 1:35 pm

    That code works fine. What do you mean by “Groovy compiler complains”?

    You can define that function with a def if you wanted, and it would return aVariable (as that is what the assignment operatop returns), however, it wouldnt be following the standard for Java Beans in that setters should return null

    Given that however, I can run:

    a = new GroovyClass()
    a.aVariable = 3
    

    And it works fine

    Edit

    Basically, it’s all down to the Groovy parser. The parser expects some sort of list of 1..N keywords defining it’s type or visibility, and then a name for the variable. So the following are all valid:

    class OkA {
      def aValue
    }
    
    class OkB {
      private aValue
    }
    
    class OkC {
      private String aValue
    }
    

    But you cannot just (with the current parser) say:

    class BadA {
      aValue
    }
    

    Thinking about it, there’s no reason I can currently think of for this restriction (as you can declare vars without def in Groovy), but the restriction is there, so you need to type def when defining class attributes.

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

Sidebar

Related Questions

I have a variable in my Grails app’s BootStrap.groovy : class BootStrap { def
class A: pass def b(self): print('b') A.b = b a = A() At this
I want to let users supply a groovy class with a property that is
Let's say that I've got a BarService under grails-app/services and regular Groovy class 'Foo'
I have a domain three domain class like this : Tasks.groovy class Tasks {
I am attempting to create a script that wraps a Groovy class that will
This was taken nearly verbatim from IBM's Mastering Grails series. DateTagLib.groovy: class DateTagLib {
I have a groovy class that has the ability to write its output to
I need to fetch events from a virtual machine in Groovy. I found this
The following that I have to use to run my groovy class file ..\bin\groovy.bat

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.