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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:08:05+00:00 2026-06-15T04:08:05+00:00

Hi apologies for the basic question, im sure I’ve been told the answer before

  • 0

Hi apologies for the basic question, im sure I’ve been told the answer before and I’ve spent some time searching but couldn’t find a good answer (probably because its hard to phrase as a search query), I’ve done a little bit of OO programming before but ive done a lot of procedural stuff recently so that seems to be clouding my thoughts.

I have a program that has to work with strings, part of that program involves sanitising a string, so I have this method:

private void sanitizeString() {
    removeEscape();
    removePunctuation();
    removeCaps();
}

And earlier in the class declared the variable

String x = "... some string ..."

In procedural you would obviously pass all of the functions the string that they need to work on, my question is in OO is it ok to have this string declared at the top of the class and then just do something like

private void removeCaps() {
    x = x.toLowerCase();
}

or should it be

private String removeCaps(String y) {
    y = y.toLowerCase();
    return y;
} 

I think this it should be the first way, and I know that that works ok, but im doing something that has to demonstrate good OO so I just want to check I have my basic assumptions right.

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-06-15T04:08:06+00:00Added an answer on June 15, 2026 at 4:08 am

    Since x is declared as class variable, this one is fine:

      private void removeCaps() {
            x = x.toLowerCase();
      }
    

    as class variables are accessible inside class methods and you don’t need to pass the class variables as arguments to same class methods

    Same class variables are accessed this way only. Very simple example could be POJO classes, where you declare class variables and expose them through getter/setter methods. You don;t need to pass the class variables to these methods and some time, you can’t(e.g. in getter methods).

    Adding some thoughts around class variable vs. local variable in the method.

    1. If there is a need of a variable which is theoretically associated with the class definition then the variable should be defined as class variable. e.g. employeeId, employeeName.. variables in a employee class should be defined as Employee class variables.

    2. If there are variable needs which are local to a class method only and not required anywhere outside the method/class, then it should be defined as local variable inside the method.

    3. If you are defining some utility methods to respond using some variables then those variables should be passed as argument to the util methods.

    Back to your question:

    If you are defining a entire class e.g. Sanitising, which has several methods around the string variable e.g. String class itself, then better to define your string as class variable and write the methods using the class variables.

    But if you are defining Sanitising as a util/helper class then better to pass the string as method argument as normally you don;t want your util methods to be statefull (associated with the class instance).

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

Sidebar

Related Questions

apologies if this is a basic question but I haven't been able to figure
My apologies for the extremely basic question, I think I know the answer but
Apologies for so basic a question, but I can't find an answer anywhere. When
Apologies for asking too basic question but I couldn't get it cleared after reading
Apologies if this is a too basic question but I couldn't find any satisfactory
This, I'm sure is a pretty basic question about JavaScript, so apologies in advance.
Apologies if this is a very basic question, but I am preparing to create
Apologies if this is too ignorant a question or has been asked before. A
Apologies for this basic question, but I am noob stumped. The below code works
Apologies to ask such a basic question but I really cant see why In3

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.