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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T02:39:19+00:00 2026-06-02T02:39:19+00:00

In class we usually declare global variables and local variables. I have most of

  • 0

In class we usually declare global variables and local variables. I have most of the time seen declaring global variables, setters ,getters. Are these essential everytime?Is it ok if I can implement it without using those things?

  • 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-02T02:39:21+00:00Added an answer on June 2, 2026 at 2:39 am

    Generally you should always try to reduce the scope of visibility of variables, methods, classes etc.

    So, if you need some variable that is used in one call sequence use local variable and method arguments to pass its value from method to method.
    For example I have 2 methods foo() and bar() while foo calls bar:

    public void foo() {
        bar();
    }
    private void bar() {
    }
    

    Let’s say both work on the same string appending to it some suffixes. You can use local variable as in the following example:

    public String foo(String s) {
        s += "foo1"
        bar(s);
        s += "foo2"
        return s;
    }
    private String bar(String s) {
        s += "bar1"
        return s;
    }
    

    Or class level variable:

    private String s;
    
    public String foo() {
        s += "foo1"
        bar();
        s += "foo2"
        return s;
    }
    private String bar() {
        s += "bar1"
        return s;
    }
    

    The first way is better because:

    1. it is encapsulated. No-one knows the detail of implementation. No-one can affect it from outside.
    2. It is easier readable: you do not have to travel back and forward into your code to understand what objects participate in the implementation.
    3. It is thread-safe without any extra efforts like synchronized blocks or atomic variable because it never uses object level variables.

    The second implementation is not encapsulated: one can add code that changes the object state and affects on the next call of foo(). You have to go back and forward in you class to understand both algorithm and what variables are affected by algorithm. It is not thread-safe. Two concurrent threads running the same code may compete on changing the same variable.

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

Sidebar

Related Questions

I have a class in a unit. Usually, when I changed the algorithm of
For me, I usually make a global class with all members static. All other
I have a dynamic class, which has an array declared normally, before runtime. Usually,
A class went to a school trip. And, as usually, all N kids have
i just create reading file feature. In general, what kind PHP class that usually
When I'm building a class library I usually create a file Enums.cs to hold
Hey I usually run into a situation where I will create a class that
When creating a class that has internal private methods, usually to reduce code duplication,
I get from another class string that must be converted to char. It usually
I've had trouble finding a clear, concise laymans definition of a class. Usually, they

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.