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

  • Home
  • SEARCH
  • 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 6105151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:55:28+00:00 2026-05-23T13:55:28+00:00

I wonder why I can’t use the same name for function parameter and the

  • 0

I wonder why I can’t use the same name for function parameter and the name used in a class. Please refer to the following example.

scala> class Person() {var name = "bob" }
defined class Person

scala> val p = new Person
p: Person = Person@486f8860

scala> p.name
res0: java.lang.String = bob

scala> p.name = "alice"

scala> p.name
res1: java.lang.String = alice

scala> def chngName(name:String) = new Person() {this.name= name}
chngName: (name: String)Person

scala> val p = chngName("aa")
p: Person = $anon$1@3fa1732d

scala> p.name
res2: java.lang.String = bob

scala> def chngName(n:String) = new Person() {name= n}
chngName: (n: String)Person

scala> val p = chngName("aa")
p: Person = $anon$1@19d2052b

scala> p.name
res3: java.lang.String = aa

Of course, I can use a different name but I want to why I can’t or there is something I am miss here. Thanks

  • 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-23T13:55:29+00:00Added an answer on May 23, 2026 at 1:55 pm

    (Why do want to use var when you return a new object in chngName?)

    As has been said, the class’s field of the same name shadows the method argument.

    You could of course rename it before entering the class’s scope:

    def chngName(name:String) = {
      val _name = name
      new Person() { name = _name }
    }
    

    For this use case, there are a few other options, however. It depends, though, whether you want to copy the object, ie. return a new Person, or if a simple change of the var suffices.

    If you want to return a completely new object, you may consider using a case class which adds a copy method with the same semantics and identically named method arguments. (So you can use named arguments):

    case class Person(name: String)
    val p = Person("bob")
    p.name // bob
    val q= p.copy(name = "aa")
    q.name // aa
    q == p // false
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wonder how can I reconcile the following error? JVM cannot use large page
wonder whether someone can help me with the following one... I have a struct
i wonder can a IO() function return tuple because i would like to get
I just wonder that can we pass any type of class instances to view
Please can you help me with advice or demo code for the following task:
I wonder how can i remove all string part after #. For example if
I wonder how can I use similar to Eclipse's remote debugging technique to get
In Python, I can use decorators to trace the function call, its variables and
Hello everybody i wonder: when can i use mapped by to indicate whose is
I wonder how can I export a Visual Studio C++ project to Qt? I

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.